{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Objective : Shaping & Structuring\n",
    "<hr>\n",
    "\n",
    "1. Pivoting\n",
    "2. Pivot Tables\n",
    "3. Stacking & Unstacking\n",
    "4. Melting\n",
    "5. GroupBy\n",
    "6. Cross Tab\n",
    "7. Tiling\n",
    "8. Computing Dummy Variables\n",
    "9. Factorize\n",
    "10. Exploding Data\n",
    "\n",
    "<hr>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "gap_data = pd.read_csv('../Data/gapminder-FiveYearData.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>country</th>\n",
       "      <th>year</th>\n",
       "      <th>pop</th>\n",
       "      <th>continent</th>\n",
       "      <th>lifeExp</th>\n",
       "      <th>gdpPercap</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>430</th>\n",
       "      <td>Djibouti</td>\n",
       "      <td>2002</td>\n",
       "      <td>447416.0</td>\n",
       "      <td>Africa</td>\n",
       "      <td>53.373</td>\n",
       "      <td>1908.260867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>830</th>\n",
       "      <td>Korea Dem. Rep.</td>\n",
       "      <td>1962</td>\n",
       "      <td>10917494.0</td>\n",
       "      <td>Asia</td>\n",
       "      <td>56.656</td>\n",
       "      <td>1621.693598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1414</th>\n",
       "      <td>South Africa</td>\n",
       "      <td>2002</td>\n",
       "      <td>44433622.0</td>\n",
       "      <td>Africa</td>\n",
       "      <td>53.365</td>\n",
       "      <td>7710.946444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>409</th>\n",
       "      <td>Denmark</td>\n",
       "      <td>1957</td>\n",
       "      <td>4487831.0</td>\n",
       "      <td>Europe</td>\n",
       "      <td>71.810</td>\n",
       "      <td>11099.659350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>960</th>\n",
       "      <td>Mauritania</td>\n",
       "      <td>1952</td>\n",
       "      <td>1022556.0</td>\n",
       "      <td>Africa</td>\n",
       "      <td>40.543</td>\n",
       "      <td>743.115910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>Algeria</td>\n",
       "      <td>1987</td>\n",
       "      <td>23254956.0</td>\n",
       "      <td>Africa</td>\n",
       "      <td>65.799</td>\n",
       "      <td>5681.358539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122</th>\n",
       "      <td>Benin</td>\n",
       "      <td>1962</td>\n",
       "      <td>2151895.0</td>\n",
       "      <td>Africa</td>\n",
       "      <td>42.618</td>\n",
       "      <td>949.499064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>753</th>\n",
       "      <td>Ireland</td>\n",
       "      <td>1997</td>\n",
       "      <td>3667233.0</td>\n",
       "      <td>Europe</td>\n",
       "      <td>76.122</td>\n",
       "      <td>24521.947130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Algeria</td>\n",
       "      <td>1977</td>\n",
       "      <td>17152804.0</td>\n",
       "      <td>Africa</td>\n",
       "      <td>58.014</td>\n",
       "      <td>4910.416756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>Honduras</td>\n",
       "      <td>1982</td>\n",
       "      <td>3669448.0</td>\n",
       "      <td>Americas</td>\n",
       "      <td>60.909</td>\n",
       "      <td>3121.760794</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              country  year         pop continent  lifeExp     gdpPercap\n",
       "430          Djibouti  2002    447416.0    Africa   53.373   1908.260867\n",
       "830   Korea Dem. Rep.  1962  10917494.0      Asia   56.656   1621.693598\n",
       "1414     South Africa  2002  44433622.0    Africa   53.365   7710.946444\n",
       "409           Denmark  1957   4487831.0    Europe   71.810  11099.659350\n",
       "960        Mauritania  1952   1022556.0    Africa   40.543    743.115910\n",
       "31            Algeria  1987  23254956.0    Africa   65.799   5681.358539\n",
       "122             Benin  1962   2151895.0    Africa   42.618    949.499064\n",
       "753           Ireland  1997   3667233.0    Europe   76.122  24521.947130\n",
       "29            Algeria  1977  17152804.0    Africa   58.014   4910.416756\n",
       "654          Honduras  1982   3669448.0  Americas   60.909   3121.760794"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gap_data.sample(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Reshaping using dataframes means the transformation of the structure of a table or vector (i.e. DataFrame or Series) to make it suitable for further analysis. We will study 10 techniques for this.\n",
    "\n",
    "### 1. Pivoting\n",
    "* Create a new derived table out of a given table.\n",
    "* Pivot() take three params, all columns. values param can have multiple columns.\n",
    "* The below table extracts relation between country year & population trend.\n",
    "* Constraint - There cannot be more than one value corresponding to (country,year) tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"12\" halign=\"left\">lifeExp</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>1952</th>\n",
       "      <th>1957</th>\n",
       "      <th>1962</th>\n",
       "      <th>1967</th>\n",
       "      <th>1972</th>\n",
       "      <th>1977</th>\n",
       "      <th>1982</th>\n",
       "      <th>1987</th>\n",
       "      <th>1992</th>\n",
       "      <th>1997</th>\n",
       "      <th>2002</th>\n",
       "      <th>2007</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>country</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Afghanistan</th>\n",
       "      <td>28.801</td>\n",
       "      <td>30.33200</td>\n",
       "      <td>31.99700</td>\n",
       "      <td>34.02000</td>\n",
       "      <td>36.08800</td>\n",
       "      <td>38.43800</td>\n",
       "      <td>39.854</td>\n",
       "      <td>40.822</td>\n",
       "      <td>41.674</td>\n",
       "      <td>41.763</td>\n",
       "      <td>42.129</td>\n",
       "      <td>43.828</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Albania</th>\n",
       "      <td>55.230</td>\n",
       "      <td>59.28000</td>\n",
       "      <td>64.82000</td>\n",
       "      <td>66.22000</td>\n",
       "      <td>67.69000</td>\n",
       "      <td>68.93000</td>\n",
       "      <td>70.420</td>\n",
       "      <td>72.000</td>\n",
       "      <td>71.581</td>\n",
       "      <td>72.950</td>\n",
       "      <td>75.651</td>\n",
       "      <td>76.423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Algeria</th>\n",
       "      <td>43.077</td>\n",
       "      <td>45.68500</td>\n",
       "      <td>48.30300</td>\n",
       "      <td>51.40700</td>\n",
       "      <td>54.51800</td>\n",
       "      <td>58.01400</td>\n",
       "      <td>61.368</td>\n",
       "      <td>65.799</td>\n",
       "      <td>67.744</td>\n",
       "      <td>69.152</td>\n",
       "      <td>70.994</td>\n",
       "      <td>72.301</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Angola</th>\n",
       "      <td>30.015</td>\n",
       "      <td>31.99900</td>\n",
       "      <td>34.00000</td>\n",
       "      <td>35.98500</td>\n",
       "      <td>37.92800</td>\n",
       "      <td>39.48300</td>\n",
       "      <td>39.942</td>\n",
       "      <td>39.906</td>\n",
       "      <td>40.647</td>\n",
       "      <td>40.963</td>\n",
       "      <td>41.003</td>\n",
       "      <td>42.731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Argentina</th>\n",
       "      <td>62.485</td>\n",
       "      <td>64.39900</td>\n",
       "      <td>65.14200</td>\n",
       "      <td>65.63400</td>\n",
       "      <td>67.06500</td>\n",
       "      <td>68.48100</td>\n",
       "      <td>69.942</td>\n",
       "      <td>70.774</td>\n",
       "      <td>71.868</td>\n",
       "      <td>73.275</td>\n",
       "      <td>74.340</td>\n",
       "      <td>75.320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Australia</th>\n",
       "      <td>69.120</td>\n",
       "      <td>70.33000</td>\n",
       "      <td>70.93000</td>\n",
       "      <td>71.10000</td>\n",
       "      <td>71.93000</td>\n",
       "      <td>73.49000</td>\n",
       "      <td>74.740</td>\n",
       "      <td>76.320</td>\n",
       "      <td>77.560</td>\n",
       "      <td>78.830</td>\n",
       "      <td>80.370</td>\n",
       "      <td>81.235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Austria</th>\n",
       "      <td>66.800</td>\n",
       "      <td>67.48000</td>\n",
       "      <td>69.54000</td>\n",
       "      <td>70.14000</td>\n",
       "      <td>70.63000</td>\n",
       "      <td>72.17000</td>\n",
       "      <td>73.180</td>\n",
       "      <td>74.940</td>\n",
       "      <td>76.040</td>\n",
       "      <td>77.510</td>\n",
       "      <td>78.980</td>\n",
       "      <td>79.829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bahrain</th>\n",
       "      <td>50.939</td>\n",
       "      <td>53.83200</td>\n",
       "      <td>56.92300</td>\n",
       "      <td>59.92300</td>\n",
       "      <td>63.30000</td>\n",
       "      <td>65.59300</td>\n",
       "      <td>69.052</td>\n",
       "      <td>70.750</td>\n",
       "      <td>72.601</td>\n",
       "      <td>73.925</td>\n",
       "      <td>74.795</td>\n",
       "      <td>75.635</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bangladesh</th>\n",
       "      <td>37.484</td>\n",
       "      <td>39.34800</td>\n",
       "      <td>41.21600</td>\n",
       "      <td>43.45300</td>\n",
       "      <td>45.25200</td>\n",
       "      <td>46.92300</td>\n",
       "      <td>50.009</td>\n",
       "      <td>52.819</td>\n",
       "      <td>56.018</td>\n",
       "      <td>59.412</td>\n",
       "      <td>62.013</td>\n",
       "      <td>64.062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Belgium</th>\n",
       "      <td>68.000</td>\n",
       "      <td>69.24000</td>\n",
       "      <td>70.25000</td>\n",
       "      <td>70.94000</td>\n",
       "      <td>71.44000</td>\n",
       "      <td>72.80000</td>\n",
       "      <td>73.930</td>\n",
       "      <td>75.350</td>\n",
       "      <td>76.460</td>\n",
       "      <td>77.530</td>\n",
       "      <td>78.320</td>\n",
       "      <td>79.441</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Benin</th>\n",
       "      <td>38.223</td>\n",
       "      <td>40.35800</td>\n",
       "      <td>42.61800</td>\n",
       "      <td>44.88500</td>\n",
       "      <td>47.01400</td>\n",
       "      <td>49.19000</td>\n",
       "      <td>50.904</td>\n",
       "      <td>52.337</td>\n",
       "      <td>53.919</td>\n",
       "      <td>54.777</td>\n",
       "      <td>54.406</td>\n",
       "      <td>56.728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bolivia</th>\n",
       "      <td>40.414</td>\n",
       "      <td>41.89000</td>\n",
       "      <td>43.42800</td>\n",
       "      <td>45.03200</td>\n",
       "      <td>46.71400</td>\n",
       "      <td>50.02300</td>\n",
       "      <td>53.859</td>\n",
       "      <td>57.251</td>\n",
       "      <td>59.957</td>\n",
       "      <td>62.050</td>\n",
       "      <td>63.883</td>\n",
       "      <td>65.554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bosnia and Herzegovina</th>\n",
       "      <td>53.820</td>\n",
       "      <td>58.45000</td>\n",
       "      <td>61.93000</td>\n",
       "      <td>64.79000</td>\n",
       "      <td>67.45000</td>\n",
       "      <td>69.86000</td>\n",
       "      <td>70.690</td>\n",
       "      <td>71.140</td>\n",
       "      <td>72.178</td>\n",
       "      <td>73.244</td>\n",
       "      <td>74.090</td>\n",
       "      <td>74.852</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Botswana</th>\n",
       "      <td>47.622</td>\n",
       "      <td>49.61800</td>\n",
       "      <td>51.52000</td>\n",
       "      <td>53.29800</td>\n",
       "      <td>56.02400</td>\n",
       "      <td>59.31900</td>\n",
       "      <td>61.484</td>\n",
       "      <td>63.622</td>\n",
       "      <td>62.745</td>\n",
       "      <td>52.556</td>\n",
       "      <td>46.634</td>\n",
       "      <td>50.728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Brazil</th>\n",
       "      <td>50.917</td>\n",
       "      <td>53.28500</td>\n",
       "      <td>55.66500</td>\n",
       "      <td>57.63200</td>\n",
       "      <td>59.50400</td>\n",
       "      <td>61.48900</td>\n",
       "      <td>63.336</td>\n",
       "      <td>65.205</td>\n",
       "      <td>67.057</td>\n",
       "      <td>69.388</td>\n",
       "      <td>71.006</td>\n",
       "      <td>72.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bulgaria</th>\n",
       "      <td>59.600</td>\n",
       "      <td>66.61000</td>\n",
       "      <td>69.51000</td>\n",
       "      <td>70.42000</td>\n",
       "      <td>70.90000</td>\n",
       "      <td>70.81000</td>\n",
       "      <td>71.080</td>\n",
       "      <td>71.340</td>\n",
       "      <td>71.190</td>\n",
       "      <td>70.320</td>\n",
       "      <td>72.140</td>\n",
       "      <td>73.005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Burkina Faso</th>\n",
       "      <td>31.975</td>\n",
       "      <td>34.90600</td>\n",
       "      <td>37.81400</td>\n",
       "      <td>40.69700</td>\n",
       "      <td>43.59100</td>\n",
       "      <td>46.13700</td>\n",
       "      <td>48.122</td>\n",
       "      <td>49.557</td>\n",
       "      <td>50.260</td>\n",
       "      <td>50.324</td>\n",
       "      <td>50.650</td>\n",
       "      <td>52.295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Burundi</th>\n",
       "      <td>39.031</td>\n",
       "      <td>40.53300</td>\n",
       "      <td>42.04500</td>\n",
       "      <td>43.54800</td>\n",
       "      <td>44.05700</td>\n",
       "      <td>45.91000</td>\n",
       "      <td>47.471</td>\n",
       "      <td>48.211</td>\n",
       "      <td>44.736</td>\n",
       "      <td>45.326</td>\n",
       "      <td>47.360</td>\n",
       "      <td>49.580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cambodia</th>\n",
       "      <td>39.417</td>\n",
       "      <td>41.36600</td>\n",
       "      <td>43.41500</td>\n",
       "      <td>45.41500</td>\n",
       "      <td>40.31700</td>\n",
       "      <td>31.22000</td>\n",
       "      <td>50.957</td>\n",
       "      <td>53.914</td>\n",
       "      <td>55.803</td>\n",
       "      <td>56.534</td>\n",
       "      <td>56.752</td>\n",
       "      <td>59.723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cameroon</th>\n",
       "      <td>38.523</td>\n",
       "      <td>40.42800</td>\n",
       "      <td>42.64300</td>\n",
       "      <td>44.79900</td>\n",
       "      <td>47.04900</td>\n",
       "      <td>49.35500</td>\n",
       "      <td>52.961</td>\n",
       "      <td>54.985</td>\n",
       "      <td>54.314</td>\n",
       "      <td>52.199</td>\n",
       "      <td>49.856</td>\n",
       "      <td>50.430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Canada</th>\n",
       "      <td>68.750</td>\n",
       "      <td>69.96000</td>\n",
       "      <td>71.30000</td>\n",
       "      <td>72.13000</td>\n",
       "      <td>72.88000</td>\n",
       "      <td>74.21000</td>\n",
       "      <td>75.760</td>\n",
       "      <td>76.860</td>\n",
       "      <td>77.950</td>\n",
       "      <td>78.610</td>\n",
       "      <td>79.770</td>\n",
       "      <td>80.653</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Central African Republic</th>\n",
       "      <td>35.463</td>\n",
       "      <td>37.46400</td>\n",
       "      <td>39.47500</td>\n",
       "      <td>41.47800</td>\n",
       "      <td>43.45700</td>\n",
       "      <td>46.77500</td>\n",
       "      <td>48.295</td>\n",
       "      <td>50.485</td>\n",
       "      <td>49.396</td>\n",
       "      <td>46.066</td>\n",
       "      <td>43.308</td>\n",
       "      <td>44.741</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chad</th>\n",
       "      <td>38.092</td>\n",
       "      <td>39.88100</td>\n",
       "      <td>41.71600</td>\n",
       "      <td>43.60100</td>\n",
       "      <td>45.56900</td>\n",
       "      <td>47.38300</td>\n",
       "      <td>49.517</td>\n",
       "      <td>51.051</td>\n",
       "      <td>51.724</td>\n",
       "      <td>51.573</td>\n",
       "      <td>50.525</td>\n",
       "      <td>50.651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chile</th>\n",
       "      <td>54.745</td>\n",
       "      <td>56.07400</td>\n",
       "      <td>57.92400</td>\n",
       "      <td>60.52300</td>\n",
       "      <td>63.44100</td>\n",
       "      <td>67.05200</td>\n",
       "      <td>70.565</td>\n",
       "      <td>72.492</td>\n",
       "      <td>74.126</td>\n",
       "      <td>75.816</td>\n",
       "      <td>77.860</td>\n",
       "      <td>78.553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>China</th>\n",
       "      <td>44.000</td>\n",
       "      <td>50.54896</td>\n",
       "      <td>44.50136</td>\n",
       "      <td>58.38112</td>\n",
       "      <td>63.11888</td>\n",
       "      <td>63.96736</td>\n",
       "      <td>65.525</td>\n",
       "      <td>67.274</td>\n",
       "      <td>68.690</td>\n",
       "      <td>70.426</td>\n",
       "      <td>72.028</td>\n",
       "      <td>72.961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Colombia</th>\n",
       "      <td>50.643</td>\n",
       "      <td>55.11800</td>\n",
       "      <td>57.86300</td>\n",
       "      <td>59.96300</td>\n",
       "      <td>61.62300</td>\n",
       "      <td>63.83700</td>\n",
       "      <td>66.653</td>\n",
       "      <td>67.768</td>\n",
       "      <td>68.421</td>\n",
       "      <td>70.313</td>\n",
       "      <td>71.682</td>\n",
       "      <td>72.889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Comoros</th>\n",
       "      <td>40.715</td>\n",
       "      <td>42.46000</td>\n",
       "      <td>44.46700</td>\n",
       "      <td>46.47200</td>\n",
       "      <td>48.94400</td>\n",
       "      <td>50.93900</td>\n",
       "      <td>52.933</td>\n",
       "      <td>54.926</td>\n",
       "      <td>57.939</td>\n",
       "      <td>60.660</td>\n",
       "      <td>62.974</td>\n",
       "      <td>65.152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Congo Dem. Rep.</th>\n",
       "      <td>39.143</td>\n",
       "      <td>40.65200</td>\n",
       "      <td>42.12200</td>\n",
       "      <td>44.05600</td>\n",
       "      <td>45.98900</td>\n",
       "      <td>47.80400</td>\n",
       "      <td>47.784</td>\n",
       "      <td>47.412</td>\n",
       "      <td>45.548</td>\n",
       "      <td>42.587</td>\n",
       "      <td>44.966</td>\n",
       "      <td>46.462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Congo Rep.</th>\n",
       "      <td>42.111</td>\n",
       "      <td>45.05300</td>\n",
       "      <td>48.43500</td>\n",
       "      <td>52.04000</td>\n",
       "      <td>54.90700</td>\n",
       "      <td>55.62500</td>\n",
       "      <td>56.695</td>\n",
       "      <td>57.470</td>\n",
       "      <td>56.433</td>\n",
       "      <td>52.962</td>\n",
       "      <td>52.970</td>\n",
       "      <td>55.322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Costa Rica</th>\n",
       "      <td>57.206</td>\n",
       "      <td>60.02600</td>\n",
       "      <td>62.84200</td>\n",
       "      <td>65.42400</td>\n",
       "      <td>67.84900</td>\n",
       "      <td>70.75000</td>\n",
       "      <td>73.450</td>\n",
       "      <td>74.752</td>\n",
       "      <td>75.713</td>\n",
       "      <td>77.260</td>\n",
       "      <td>78.123</td>\n",
       "      <td>78.782</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sierra Leone</th>\n",
       "      <td>30.331</td>\n",
       "      <td>31.57000</td>\n",
       "      <td>32.76700</td>\n",
       "      <td>34.11300</td>\n",
       "      <td>35.40000</td>\n",
       "      <td>36.78800</td>\n",
       "      <td>38.445</td>\n",
       "      <td>40.006</td>\n",
       "      <td>38.333</td>\n",
       "      <td>39.897</td>\n",
       "      <td>41.012</td>\n",
       "      <td>42.568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Singapore</th>\n",
       "      <td>60.396</td>\n",
       "      <td>63.17900</td>\n",
       "      <td>65.79800</td>\n",
       "      <td>67.94600</td>\n",
       "      <td>69.52100</td>\n",
       "      <td>70.79500</td>\n",
       "      <td>71.760</td>\n",
       "      <td>73.560</td>\n",
       "      <td>75.788</td>\n",
       "      <td>77.158</td>\n",
       "      <td>78.770</td>\n",
       "      <td>79.972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Slovak Republic</th>\n",
       "      <td>64.360</td>\n",
       "      <td>67.45000</td>\n",
       "      <td>70.33000</td>\n",
       "      <td>70.98000</td>\n",
       "      <td>70.35000</td>\n",
       "      <td>70.45000</td>\n",
       "      <td>70.800</td>\n",
       "      <td>71.080</td>\n",
       "      <td>71.380</td>\n",
       "      <td>72.710</td>\n",
       "      <td>73.800</td>\n",
       "      <td>74.663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Slovenia</th>\n",
       "      <td>65.570</td>\n",
       "      <td>67.85000</td>\n",
       "      <td>69.15000</td>\n",
       "      <td>69.18000</td>\n",
       "      <td>69.82000</td>\n",
       "      <td>70.97000</td>\n",
       "      <td>71.063</td>\n",
       "      <td>72.250</td>\n",
       "      <td>73.640</td>\n",
       "      <td>75.130</td>\n",
       "      <td>76.660</td>\n",
       "      <td>77.926</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Somalia</th>\n",
       "      <td>32.978</td>\n",
       "      <td>34.97700</td>\n",
       "      <td>36.98100</td>\n",
       "      <td>38.97700</td>\n",
       "      <td>40.97300</td>\n",
       "      <td>41.97400</td>\n",
       "      <td>42.955</td>\n",
       "      <td>44.501</td>\n",
       "      <td>39.658</td>\n",
       "      <td>43.795</td>\n",
       "      <td>45.936</td>\n",
       "      <td>48.159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South Africa</th>\n",
       "      <td>45.009</td>\n",
       "      <td>47.98500</td>\n",
       "      <td>49.95100</td>\n",
       "      <td>51.92700</td>\n",
       "      <td>53.69600</td>\n",
       "      <td>55.52700</td>\n",
       "      <td>58.161</td>\n",
       "      <td>60.834</td>\n",
       "      <td>61.888</td>\n",
       "      <td>60.236</td>\n",
       "      <td>53.365</td>\n",
       "      <td>49.339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spain</th>\n",
       "      <td>64.940</td>\n",
       "      <td>66.66000</td>\n",
       "      <td>69.69000</td>\n",
       "      <td>71.44000</td>\n",
       "      <td>73.06000</td>\n",
       "      <td>74.39000</td>\n",
       "      <td>76.300</td>\n",
       "      <td>76.900</td>\n",
       "      <td>77.570</td>\n",
       "      <td>78.770</td>\n",
       "      <td>79.780</td>\n",
       "      <td>80.941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sri Lanka</th>\n",
       "      <td>57.593</td>\n",
       "      <td>61.45600</td>\n",
       "      <td>62.19200</td>\n",
       "      <td>64.26600</td>\n",
       "      <td>65.04200</td>\n",
       "      <td>65.94900</td>\n",
       "      <td>68.757</td>\n",
       "      <td>69.011</td>\n",
       "      <td>70.379</td>\n",
       "      <td>70.457</td>\n",
       "      <td>70.815</td>\n",
       "      <td>72.396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sudan</th>\n",
       "      <td>38.635</td>\n",
       "      <td>39.62400</td>\n",
       "      <td>40.87000</td>\n",
       "      <td>42.85800</td>\n",
       "      <td>45.08300</td>\n",
       "      <td>47.80000</td>\n",
       "      <td>50.338</td>\n",
       "      <td>51.744</td>\n",
       "      <td>53.556</td>\n",
       "      <td>55.373</td>\n",
       "      <td>56.369</td>\n",
       "      <td>58.556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Swaziland</th>\n",
       "      <td>41.407</td>\n",
       "      <td>43.42400</td>\n",
       "      <td>44.99200</td>\n",
       "      <td>46.63300</td>\n",
       "      <td>49.55200</td>\n",
       "      <td>52.53700</td>\n",
       "      <td>55.561</td>\n",
       "      <td>57.678</td>\n",
       "      <td>58.474</td>\n",
       "      <td>54.289</td>\n",
       "      <td>43.869</td>\n",
       "      <td>39.613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sweden</th>\n",
       "      <td>71.860</td>\n",
       "      <td>72.49000</td>\n",
       "      <td>73.37000</td>\n",
       "      <td>74.16000</td>\n",
       "      <td>74.72000</td>\n",
       "      <td>75.44000</td>\n",
       "      <td>76.420</td>\n",
       "      <td>77.190</td>\n",
       "      <td>78.160</td>\n",
       "      <td>79.390</td>\n",
       "      <td>80.040</td>\n",
       "      <td>80.884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Switzerland</th>\n",
       "      <td>69.620</td>\n",
       "      <td>70.56000</td>\n",
       "      <td>71.32000</td>\n",
       "      <td>72.77000</td>\n",
       "      <td>73.78000</td>\n",
       "      <td>75.39000</td>\n",
       "      <td>76.210</td>\n",
       "      <td>77.410</td>\n",
       "      <td>78.030</td>\n",
       "      <td>79.370</td>\n",
       "      <td>80.620</td>\n",
       "      <td>81.701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Syria</th>\n",
       "      <td>45.883</td>\n",
       "      <td>48.28400</td>\n",
       "      <td>50.30500</td>\n",
       "      <td>53.65500</td>\n",
       "      <td>57.29600</td>\n",
       "      <td>61.19500</td>\n",
       "      <td>64.590</td>\n",
       "      <td>66.974</td>\n",
       "      <td>69.249</td>\n",
       "      <td>71.527</td>\n",
       "      <td>73.053</td>\n",
       "      <td>74.143</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Taiwan</th>\n",
       "      <td>58.500</td>\n",
       "      <td>62.40000</td>\n",
       "      <td>65.20000</td>\n",
       "      <td>67.50000</td>\n",
       "      <td>69.39000</td>\n",
       "      <td>70.59000</td>\n",
       "      <td>72.160</td>\n",
       "      <td>73.400</td>\n",
       "      <td>74.260</td>\n",
       "      <td>75.250</td>\n",
       "      <td>76.990</td>\n",
       "      <td>78.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tanzania</th>\n",
       "      <td>41.215</td>\n",
       "      <td>42.97400</td>\n",
       "      <td>44.24600</td>\n",
       "      <td>45.75700</td>\n",
       "      <td>47.62000</td>\n",
       "      <td>49.91900</td>\n",
       "      <td>50.608</td>\n",
       "      <td>51.535</td>\n",
       "      <td>50.440</td>\n",
       "      <td>48.466</td>\n",
       "      <td>49.651</td>\n",
       "      <td>52.517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Thailand</th>\n",
       "      <td>50.848</td>\n",
       "      <td>53.63000</td>\n",
       "      <td>56.06100</td>\n",
       "      <td>58.28500</td>\n",
       "      <td>60.40500</td>\n",
       "      <td>62.49400</td>\n",
       "      <td>64.597</td>\n",
       "      <td>66.084</td>\n",
       "      <td>67.298</td>\n",
       "      <td>67.521</td>\n",
       "      <td>68.564</td>\n",
       "      <td>70.616</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Togo</th>\n",
       "      <td>38.596</td>\n",
       "      <td>41.20800</td>\n",
       "      <td>43.92200</td>\n",
       "      <td>46.76900</td>\n",
       "      <td>49.75900</td>\n",
       "      <td>52.88700</td>\n",
       "      <td>55.471</td>\n",
       "      <td>56.941</td>\n",
       "      <td>58.061</td>\n",
       "      <td>58.390</td>\n",
       "      <td>57.561</td>\n",
       "      <td>58.420</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Trinidad and Tobago</th>\n",
       "      <td>59.100</td>\n",
       "      <td>61.80000</td>\n",
       "      <td>64.90000</td>\n",
       "      <td>65.40000</td>\n",
       "      <td>65.90000</td>\n",
       "      <td>68.30000</td>\n",
       "      <td>68.832</td>\n",
       "      <td>69.582</td>\n",
       "      <td>69.862</td>\n",
       "      <td>69.465</td>\n",
       "      <td>68.976</td>\n",
       "      <td>69.819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tunisia</th>\n",
       "      <td>44.600</td>\n",
       "      <td>47.10000</td>\n",
       "      <td>49.57900</td>\n",
       "      <td>52.05300</td>\n",
       "      <td>55.60200</td>\n",
       "      <td>59.83700</td>\n",
       "      <td>64.048</td>\n",
       "      <td>66.894</td>\n",
       "      <td>70.001</td>\n",
       "      <td>71.973</td>\n",
       "      <td>73.042</td>\n",
       "      <td>73.923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Turkey</th>\n",
       "      <td>43.585</td>\n",
       "      <td>48.07900</td>\n",
       "      <td>52.09800</td>\n",
       "      <td>54.33600</td>\n",
       "      <td>57.00500</td>\n",
       "      <td>59.50700</td>\n",
       "      <td>61.036</td>\n",
       "      <td>63.108</td>\n",
       "      <td>66.146</td>\n",
       "      <td>68.835</td>\n",
       "      <td>70.845</td>\n",
       "      <td>71.777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Uganda</th>\n",
       "      <td>39.978</td>\n",
       "      <td>42.57100</td>\n",
       "      <td>45.34400</td>\n",
       "      <td>48.05100</td>\n",
       "      <td>51.01600</td>\n",
       "      <td>50.35000</td>\n",
       "      <td>49.849</td>\n",
       "      <td>51.509</td>\n",
       "      <td>48.825</td>\n",
       "      <td>44.578</td>\n",
       "      <td>47.813</td>\n",
       "      <td>51.542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>United Kingdom</th>\n",
       "      <td>69.180</td>\n",
       "      <td>70.42000</td>\n",
       "      <td>70.76000</td>\n",
       "      <td>71.36000</td>\n",
       "      <td>72.01000</td>\n",
       "      <td>72.76000</td>\n",
       "      <td>74.040</td>\n",
       "      <td>75.007</td>\n",
       "      <td>76.420</td>\n",
       "      <td>77.218</td>\n",
       "      <td>78.471</td>\n",
       "      <td>79.425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>United States</th>\n",
       "      <td>68.440</td>\n",
       "      <td>69.49000</td>\n",
       "      <td>70.21000</td>\n",
       "      <td>70.76000</td>\n",
       "      <td>71.34000</td>\n",
       "      <td>73.38000</td>\n",
       "      <td>74.650</td>\n",
       "      <td>75.020</td>\n",
       "      <td>76.090</td>\n",
       "      <td>76.810</td>\n",
       "      <td>77.310</td>\n",
       "      <td>78.242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Uruguay</th>\n",
       "      <td>66.071</td>\n",
       "      <td>67.04400</td>\n",
       "      <td>68.25300</td>\n",
       "      <td>68.46800</td>\n",
       "      <td>68.67300</td>\n",
       "      <td>69.48100</td>\n",
       "      <td>70.805</td>\n",
       "      <td>71.918</td>\n",
       "      <td>72.752</td>\n",
       "      <td>74.223</td>\n",
       "      <td>75.307</td>\n",
       "      <td>76.384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Venezuela</th>\n",
       "      <td>55.088</td>\n",
       "      <td>57.90700</td>\n",
       "      <td>60.77000</td>\n",
       "      <td>63.47900</td>\n",
       "      <td>65.71200</td>\n",
       "      <td>67.45600</td>\n",
       "      <td>68.557</td>\n",
       "      <td>70.190</td>\n",
       "      <td>71.150</td>\n",
       "      <td>72.146</td>\n",
       "      <td>72.766</td>\n",
       "      <td>73.747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Vietnam</th>\n",
       "      <td>40.412</td>\n",
       "      <td>42.88700</td>\n",
       "      <td>45.36300</td>\n",
       "      <td>47.83800</td>\n",
       "      <td>50.25400</td>\n",
       "      <td>55.76400</td>\n",
       "      <td>58.816</td>\n",
       "      <td>62.820</td>\n",
       "      <td>67.662</td>\n",
       "      <td>70.672</td>\n",
       "      <td>73.017</td>\n",
       "      <td>74.249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>West Bank and Gaza</th>\n",
       "      <td>43.160</td>\n",
       "      <td>45.67100</td>\n",
       "      <td>48.12700</td>\n",
       "      <td>51.63100</td>\n",
       "      <td>56.53200</td>\n",
       "      <td>60.76500</td>\n",
       "      <td>64.406</td>\n",
       "      <td>67.046</td>\n",
       "      <td>69.718</td>\n",
       "      <td>71.096</td>\n",
       "      <td>72.370</td>\n",
       "      <td>73.422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Yemen Rep.</th>\n",
       "      <td>32.548</td>\n",
       "      <td>33.97000</td>\n",
       "      <td>35.18000</td>\n",
       "      <td>36.98400</td>\n",
       "      <td>39.84800</td>\n",
       "      <td>44.17500</td>\n",
       "      <td>49.113</td>\n",
       "      <td>52.922</td>\n",
       "      <td>55.599</td>\n",
       "      <td>58.020</td>\n",
       "      <td>60.308</td>\n",
       "      <td>62.698</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Zambia</th>\n",
       "      <td>42.038</td>\n",
       "      <td>44.07700</td>\n",
       "      <td>46.02300</td>\n",
       "      <td>47.76800</td>\n",
       "      <td>50.10700</td>\n",
       "      <td>51.38600</td>\n",
       "      <td>51.821</td>\n",
       "      <td>50.821</td>\n",
       "      <td>46.100</td>\n",
       "      <td>40.238</td>\n",
       "      <td>39.193</td>\n",
       "      <td>42.384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Zimbabwe</th>\n",
       "      <td>48.451</td>\n",
       "      <td>50.46900</td>\n",
       "      <td>52.35800</td>\n",
       "      <td>53.99500</td>\n",
       "      <td>55.63500</td>\n",
       "      <td>57.67400</td>\n",
       "      <td>60.363</td>\n",
       "      <td>62.351</td>\n",
       "      <td>60.377</td>\n",
       "      <td>46.809</td>\n",
       "      <td>39.989</td>\n",
       "      <td>43.487</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>142 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                         lifeExp                                          \\\n",
       "year                        1952      1957      1962      1967      1972   \n",
       "country                                                                    \n",
       "Afghanistan               28.801  30.33200  31.99700  34.02000  36.08800   \n",
       "Albania                   55.230  59.28000  64.82000  66.22000  67.69000   \n",
       "Algeria                   43.077  45.68500  48.30300  51.40700  54.51800   \n",
       "Angola                    30.015  31.99900  34.00000  35.98500  37.92800   \n",
       "Argentina                 62.485  64.39900  65.14200  65.63400  67.06500   \n",
       "Australia                 69.120  70.33000  70.93000  71.10000  71.93000   \n",
       "Austria                   66.800  67.48000  69.54000  70.14000  70.63000   \n",
       "Bahrain                   50.939  53.83200  56.92300  59.92300  63.30000   \n",
       "Bangladesh                37.484  39.34800  41.21600  43.45300  45.25200   \n",
       "Belgium                   68.000  69.24000  70.25000  70.94000  71.44000   \n",
       "Benin                     38.223  40.35800  42.61800  44.88500  47.01400   \n",
       "Bolivia                   40.414  41.89000  43.42800  45.03200  46.71400   \n",
       "Bosnia and Herzegovina    53.820  58.45000  61.93000  64.79000  67.45000   \n",
       "Botswana                  47.622  49.61800  51.52000  53.29800  56.02400   \n",
       "Brazil                    50.917  53.28500  55.66500  57.63200  59.50400   \n",
       "Bulgaria                  59.600  66.61000  69.51000  70.42000  70.90000   \n",
       "Burkina Faso              31.975  34.90600  37.81400  40.69700  43.59100   \n",
       "Burundi                   39.031  40.53300  42.04500  43.54800  44.05700   \n",
       "Cambodia                  39.417  41.36600  43.41500  45.41500  40.31700   \n",
       "Cameroon                  38.523  40.42800  42.64300  44.79900  47.04900   \n",
       "Canada                    68.750  69.96000  71.30000  72.13000  72.88000   \n",
       "Central African Republic  35.463  37.46400  39.47500  41.47800  43.45700   \n",
       "Chad                      38.092  39.88100  41.71600  43.60100  45.56900   \n",
       "Chile                     54.745  56.07400  57.92400  60.52300  63.44100   \n",
       "China                     44.000  50.54896  44.50136  58.38112  63.11888   \n",
       "Colombia                  50.643  55.11800  57.86300  59.96300  61.62300   \n",
       "Comoros                   40.715  42.46000  44.46700  46.47200  48.94400   \n",
       "Congo Dem. Rep.           39.143  40.65200  42.12200  44.05600  45.98900   \n",
       "Congo Rep.                42.111  45.05300  48.43500  52.04000  54.90700   \n",
       "Costa Rica                57.206  60.02600  62.84200  65.42400  67.84900   \n",
       "...                          ...       ...       ...       ...       ...   \n",
       "Sierra Leone              30.331  31.57000  32.76700  34.11300  35.40000   \n",
       "Singapore                 60.396  63.17900  65.79800  67.94600  69.52100   \n",
       "Slovak Republic           64.360  67.45000  70.33000  70.98000  70.35000   \n",
       "Slovenia                  65.570  67.85000  69.15000  69.18000  69.82000   \n",
       "Somalia                   32.978  34.97700  36.98100  38.97700  40.97300   \n",
       "South Africa              45.009  47.98500  49.95100  51.92700  53.69600   \n",
       "Spain                     64.940  66.66000  69.69000  71.44000  73.06000   \n",
       "Sri Lanka                 57.593  61.45600  62.19200  64.26600  65.04200   \n",
       "Sudan                     38.635  39.62400  40.87000  42.85800  45.08300   \n",
       "Swaziland                 41.407  43.42400  44.99200  46.63300  49.55200   \n",
       "Sweden                    71.860  72.49000  73.37000  74.16000  74.72000   \n",
       "Switzerland               69.620  70.56000  71.32000  72.77000  73.78000   \n",
       "Syria                     45.883  48.28400  50.30500  53.65500  57.29600   \n",
       "Taiwan                    58.500  62.40000  65.20000  67.50000  69.39000   \n",
       "Tanzania                  41.215  42.97400  44.24600  45.75700  47.62000   \n",
       "Thailand                  50.848  53.63000  56.06100  58.28500  60.40500   \n",
       "Togo                      38.596  41.20800  43.92200  46.76900  49.75900   \n",
       "Trinidad and Tobago       59.100  61.80000  64.90000  65.40000  65.90000   \n",
       "Tunisia                   44.600  47.10000  49.57900  52.05300  55.60200   \n",
       "Turkey                    43.585  48.07900  52.09800  54.33600  57.00500   \n",
       "Uganda                    39.978  42.57100  45.34400  48.05100  51.01600   \n",
       "United Kingdom            69.180  70.42000  70.76000  71.36000  72.01000   \n",
       "United States             68.440  69.49000  70.21000  70.76000  71.34000   \n",
       "Uruguay                   66.071  67.04400  68.25300  68.46800  68.67300   \n",
       "Venezuela                 55.088  57.90700  60.77000  63.47900  65.71200   \n",
       "Vietnam                   40.412  42.88700  45.36300  47.83800  50.25400   \n",
       "West Bank and Gaza        43.160  45.67100  48.12700  51.63100  56.53200   \n",
       "Yemen Rep.                32.548  33.97000  35.18000  36.98400  39.84800   \n",
       "Zambia                    42.038  44.07700  46.02300  47.76800  50.10700   \n",
       "Zimbabwe                  48.451  50.46900  52.35800  53.99500  55.63500   \n",
       "\n",
       "                                                                            \\\n",
       "year                          1977    1982    1987    1992    1997    2002   \n",
       "country                                                                      \n",
       "Afghanistan               38.43800  39.854  40.822  41.674  41.763  42.129   \n",
       "Albania                   68.93000  70.420  72.000  71.581  72.950  75.651   \n",
       "Algeria                   58.01400  61.368  65.799  67.744  69.152  70.994   \n",
       "Angola                    39.48300  39.942  39.906  40.647  40.963  41.003   \n",
       "Argentina                 68.48100  69.942  70.774  71.868  73.275  74.340   \n",
       "Australia                 73.49000  74.740  76.320  77.560  78.830  80.370   \n",
       "Austria                   72.17000  73.180  74.940  76.040  77.510  78.980   \n",
       "Bahrain                   65.59300  69.052  70.750  72.601  73.925  74.795   \n",
       "Bangladesh                46.92300  50.009  52.819  56.018  59.412  62.013   \n",
       "Belgium                   72.80000  73.930  75.350  76.460  77.530  78.320   \n",
       "Benin                     49.19000  50.904  52.337  53.919  54.777  54.406   \n",
       "Bolivia                   50.02300  53.859  57.251  59.957  62.050  63.883   \n",
       "Bosnia and Herzegovina    69.86000  70.690  71.140  72.178  73.244  74.090   \n",
       "Botswana                  59.31900  61.484  63.622  62.745  52.556  46.634   \n",
       "Brazil                    61.48900  63.336  65.205  67.057  69.388  71.006   \n",
       "Bulgaria                  70.81000  71.080  71.340  71.190  70.320  72.140   \n",
       "Burkina Faso              46.13700  48.122  49.557  50.260  50.324  50.650   \n",
       "Burundi                   45.91000  47.471  48.211  44.736  45.326  47.360   \n",
       "Cambodia                  31.22000  50.957  53.914  55.803  56.534  56.752   \n",
       "Cameroon                  49.35500  52.961  54.985  54.314  52.199  49.856   \n",
       "Canada                    74.21000  75.760  76.860  77.950  78.610  79.770   \n",
       "Central African Republic  46.77500  48.295  50.485  49.396  46.066  43.308   \n",
       "Chad                      47.38300  49.517  51.051  51.724  51.573  50.525   \n",
       "Chile                     67.05200  70.565  72.492  74.126  75.816  77.860   \n",
       "China                     63.96736  65.525  67.274  68.690  70.426  72.028   \n",
       "Colombia                  63.83700  66.653  67.768  68.421  70.313  71.682   \n",
       "Comoros                   50.93900  52.933  54.926  57.939  60.660  62.974   \n",
       "Congo Dem. Rep.           47.80400  47.784  47.412  45.548  42.587  44.966   \n",
       "Congo Rep.                55.62500  56.695  57.470  56.433  52.962  52.970   \n",
       "Costa Rica                70.75000  73.450  74.752  75.713  77.260  78.123   \n",
       "...                            ...     ...     ...     ...     ...     ...   \n",
       "Sierra Leone              36.78800  38.445  40.006  38.333  39.897  41.012   \n",
       "Singapore                 70.79500  71.760  73.560  75.788  77.158  78.770   \n",
       "Slovak Republic           70.45000  70.800  71.080  71.380  72.710  73.800   \n",
       "Slovenia                  70.97000  71.063  72.250  73.640  75.130  76.660   \n",
       "Somalia                   41.97400  42.955  44.501  39.658  43.795  45.936   \n",
       "South Africa              55.52700  58.161  60.834  61.888  60.236  53.365   \n",
       "Spain                     74.39000  76.300  76.900  77.570  78.770  79.780   \n",
       "Sri Lanka                 65.94900  68.757  69.011  70.379  70.457  70.815   \n",
       "Sudan                     47.80000  50.338  51.744  53.556  55.373  56.369   \n",
       "Swaziland                 52.53700  55.561  57.678  58.474  54.289  43.869   \n",
       "Sweden                    75.44000  76.420  77.190  78.160  79.390  80.040   \n",
       "Switzerland               75.39000  76.210  77.410  78.030  79.370  80.620   \n",
       "Syria                     61.19500  64.590  66.974  69.249  71.527  73.053   \n",
       "Taiwan                    70.59000  72.160  73.400  74.260  75.250  76.990   \n",
       "Tanzania                  49.91900  50.608  51.535  50.440  48.466  49.651   \n",
       "Thailand                  62.49400  64.597  66.084  67.298  67.521  68.564   \n",
       "Togo                      52.88700  55.471  56.941  58.061  58.390  57.561   \n",
       "Trinidad and Tobago       68.30000  68.832  69.582  69.862  69.465  68.976   \n",
       "Tunisia                   59.83700  64.048  66.894  70.001  71.973  73.042   \n",
       "Turkey                    59.50700  61.036  63.108  66.146  68.835  70.845   \n",
       "Uganda                    50.35000  49.849  51.509  48.825  44.578  47.813   \n",
       "United Kingdom            72.76000  74.040  75.007  76.420  77.218  78.471   \n",
       "United States             73.38000  74.650  75.020  76.090  76.810  77.310   \n",
       "Uruguay                   69.48100  70.805  71.918  72.752  74.223  75.307   \n",
       "Venezuela                 67.45600  68.557  70.190  71.150  72.146  72.766   \n",
       "Vietnam                   55.76400  58.816  62.820  67.662  70.672  73.017   \n",
       "West Bank and Gaza        60.76500  64.406  67.046  69.718  71.096  72.370   \n",
       "Yemen Rep.                44.17500  49.113  52.922  55.599  58.020  60.308   \n",
       "Zambia                    51.38600  51.821  50.821  46.100  40.238  39.193   \n",
       "Zimbabwe                  57.67400  60.363  62.351  60.377  46.809  39.989   \n",
       "\n",
       "                                  \n",
       "year                        2007  \n",
       "country                           \n",
       "Afghanistan               43.828  \n",
       "Albania                   76.423  \n",
       "Algeria                   72.301  \n",
       "Angola                    42.731  \n",
       "Argentina                 75.320  \n",
       "Australia                 81.235  \n",
       "Austria                   79.829  \n",
       "Bahrain                   75.635  \n",
       "Bangladesh                64.062  \n",
       "Belgium                   79.441  \n",
       "Benin                     56.728  \n",
       "Bolivia                   65.554  \n",
       "Bosnia and Herzegovina    74.852  \n",
       "Botswana                  50.728  \n",
       "Brazil                    72.390  \n",
       "Bulgaria                  73.005  \n",
       "Burkina Faso              52.295  \n",
       "Burundi                   49.580  \n",
       "Cambodia                  59.723  \n",
       "Cameroon                  50.430  \n",
       "Canada                    80.653  \n",
       "Central African Republic  44.741  \n",
       "Chad                      50.651  \n",
       "Chile                     78.553  \n",
       "China                     72.961  \n",
       "Colombia                  72.889  \n",
       "Comoros                   65.152  \n",
       "Congo Dem. Rep.           46.462  \n",
       "Congo Rep.                55.322  \n",
       "Costa Rica                78.782  \n",
       "...                          ...  \n",
       "Sierra Leone              42.568  \n",
       "Singapore                 79.972  \n",
       "Slovak Republic           74.663  \n",
       "Slovenia                  77.926  \n",
       "Somalia                   48.159  \n",
       "South Africa              49.339  \n",
       "Spain                     80.941  \n",
       "Sri Lanka                 72.396  \n",
       "Sudan                     58.556  \n",
       "Swaziland                 39.613  \n",
       "Sweden                    80.884  \n",
       "Switzerland               81.701  \n",
       "Syria                     74.143  \n",
       "Taiwan                    78.400  \n",
       "Tanzania                  52.517  \n",
       "Thailand                  70.616  \n",
       "Togo                      58.420  \n",
       "Trinidad and Tobago       69.819  \n",
       "Tunisia                   73.923  \n",
       "Turkey                    71.777  \n",
       "Uganda                    51.542  \n",
       "United Kingdom            79.425  \n",
       "United States             78.242  \n",
       "Uruguay                   76.384  \n",
       "Venezuela                 73.747  \n",
       "Vietnam                   74.249  \n",
       "West Bank and Gaza        73.422  \n",
       "Yemen Rep.                62.698  \n",
       "Zambia                    42.384  \n",
       "Zimbabwe                  43.487  \n",
       "\n",
       "[142 rows x 12 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gap_data.pivot(index='country',columns='year', values=['lifeExp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Index contains duplicate entries, cannot reshape",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-22-de1314825780>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#Error: Since multiple values for tuple (continent,year)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mgap_data\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpivot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'continent'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'year'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'lifeExp'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36mpivot\u001b[0;34m(self, index, columns, values)\u001b[0m\n\u001b[1;32m   5626\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   5627\u001b[0m         \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpivot\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5628\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   5629\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   5630\u001b[0m     _shared_docs['pivot_table'] = \"\"\"\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/pivot.py\u001b[0m in \u001b[0;36mpivot\u001b[0;34m(data, index, columns, values)\u001b[0m\n\u001b[1;32m    386\u001b[0m             indexed = data._constructor_sliced(data[values].values,\n\u001b[1;32m    387\u001b[0m                                                index=index)\n\u001b[0;32m--> 388\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mindexed\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    389\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    390\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36munstack\u001b[0;34m(self, level, fill_value)\u001b[0m\n\u001b[1;32m   5990\u001b[0m         \"\"\"\n\u001b[1;32m   5991\u001b[0m         \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0munstack\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5992\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0munstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   5993\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   5994\u001b[0m     _shared_docs['melt'] = (\"\"\"\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/reshape.py\u001b[0m in \u001b[0;36munstack\u001b[0;34m(obj, level, fill_value)\u001b[0m\n\u001b[1;32m    386\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mDataFrame\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    387\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mMultiIndex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 388\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0m_unstack_frame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfill_value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    389\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    390\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdropna\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/reshape.py\u001b[0m in \u001b[0;36m_unstack_frame\u001b[0;34m(obj, level, fill_value)\u001b[0m\n\u001b[1;32m    409\u001b[0m                                \u001b[0mvalue_columns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    410\u001b[0m                                \u001b[0mfill_value\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfill_value\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 411\u001b[0;31m                                constructor=obj._constructor)\n\u001b[0m\u001b[1;32m    412\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0munstacker\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    413\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/reshape.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, values, index, level, value_columns, fill_value, constructor)\u001b[0m\n\u001b[1;32m    126\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    127\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_sorted_values_labels\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 128\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_selectors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    129\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    130\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_make_sorted_values_labels\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/reshape.py\u001b[0m in \u001b[0;36m_make_selectors\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    164\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    165\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mmask\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 166\u001b[0;31m             raise ValueError('Index contains duplicate entries, '\n\u001b[0m\u001b[1;32m    167\u001b[0m                              'cannot reshape')\n\u001b[1;32m    168\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: Index contains duplicate entries, cannot reshape"
     ]
    }
   ],
   "source": [
    "#Error: Since multiple values for tuple (continent,year)\n",
    "gap_data.pivot(index='continent',columns='year', values=['lifeExp'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Pivot Table\n",
    "* Pivot table have solution to the previous problem.\n",
    "* It have the ability to aggregate overlapping values.\n",
    "* The previous data have continents which have repeating information from multiple companies\n",
    "* The aggregate function is sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>year</th>\n",
       "      <th>1952</th>\n",
       "      <th>1957</th>\n",
       "      <th>1962</th>\n",
       "      <th>1967</th>\n",
       "      <th>1972</th>\n",
       "      <th>1977</th>\n",
       "      <th>1982</th>\n",
       "      <th>1987</th>\n",
       "      <th>1992</th>\n",
       "      <th>1997</th>\n",
       "      <th>2002</th>\n",
       "      <th>2007</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>continent</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Africa</th>\n",
       "      <td>2.376405e+08</td>\n",
       "      <td>2.648377e+08</td>\n",
       "      <td>2.965169e+08</td>\n",
       "      <td>3.352895e+08</td>\n",
       "      <td>3.798795e+08</td>\n",
       "      <td>4.330610e+08</td>\n",
       "      <td>4.993486e+08</td>\n",
       "      <td>5.748341e+08</td>\n",
       "      <td>6.590815e+08</td>\n",
       "      <td>7.438330e+08</td>\n",
       "      <td>8.337239e+08</td>\n",
       "      <td>9.295397e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Americas</th>\n",
       "      <td>3.451524e+08</td>\n",
       "      <td>3.869539e+08</td>\n",
       "      <td>4.332703e+08</td>\n",
       "      <td>4.807466e+08</td>\n",
       "      <td>5.293842e+08</td>\n",
       "      <td>5.780677e+08</td>\n",
       "      <td>6.302909e+08</td>\n",
       "      <td>6.827540e+08</td>\n",
       "      <td>7.392741e+08</td>\n",
       "      <td>7.969004e+08</td>\n",
       "      <td>8.497728e+08</td>\n",
       "      <td>8.988712e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Asia</th>\n",
       "      <td>1.395357e+09</td>\n",
       "      <td>1.562781e+09</td>\n",
       "      <td>1.696357e+09</td>\n",
       "      <td>1.905663e+09</td>\n",
       "      <td>2.150972e+09</td>\n",
       "      <td>2.384514e+09</td>\n",
       "      <td>2.610136e+09</td>\n",
       "      <td>2.871221e+09</td>\n",
       "      <td>3.133292e+09</td>\n",
       "      <td>3.383286e+09</td>\n",
       "      <td>3.601802e+09</td>\n",
       "      <td>3.811954e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Europe</th>\n",
       "      <td>4.181208e+08</td>\n",
       "      <td>4.378904e+08</td>\n",
       "      <td>4.603552e+08</td>\n",
       "      <td>4.811790e+08</td>\n",
       "      <td>5.006351e+08</td>\n",
       "      <td>5.171645e+08</td>\n",
       "      <td>5.312669e+08</td>\n",
       "      <td>5.430942e+08</td>\n",
       "      <td>5.581428e+08</td>\n",
       "      <td>5.689441e+08</td>\n",
       "      <td>5.782239e+08</td>\n",
       "      <td>5.860985e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oceania</th>\n",
       "      <td>1.068601e+07</td>\n",
       "      <td>1.194198e+07</td>\n",
       "      <td>1.328352e+07</td>\n",
       "      <td>1.460041e+07</td>\n",
       "      <td>1.610610e+07</td>\n",
       "      <td>1.723900e+07</td>\n",
       "      <td>1.839485e+07</td>\n",
       "      <td>1.957442e+07</td>\n",
       "      <td>2.091965e+07</td>\n",
       "      <td>2.224143e+07</td>\n",
       "      <td>2.345483e+07</td>\n",
       "      <td>2.454995e+07</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "year               1952          1957          1962          1967  \\\n",
       "continent                                                           \n",
       "Africa     2.376405e+08  2.648377e+08  2.965169e+08  3.352895e+08   \n",
       "Americas   3.451524e+08  3.869539e+08  4.332703e+08  4.807466e+08   \n",
       "Asia       1.395357e+09  1.562781e+09  1.696357e+09  1.905663e+09   \n",
       "Europe     4.181208e+08  4.378904e+08  4.603552e+08  4.811790e+08   \n",
       "Oceania    1.068601e+07  1.194198e+07  1.328352e+07  1.460041e+07   \n",
       "\n",
       "year               1972          1977          1982          1987  \\\n",
       "continent                                                           \n",
       "Africa     3.798795e+08  4.330610e+08  4.993486e+08  5.748341e+08   \n",
       "Americas   5.293842e+08  5.780677e+08  6.302909e+08  6.827540e+08   \n",
       "Asia       2.150972e+09  2.384514e+09  2.610136e+09  2.871221e+09   \n",
       "Europe     5.006351e+08  5.171645e+08  5.312669e+08  5.430942e+08   \n",
       "Oceania    1.610610e+07  1.723900e+07  1.839485e+07  1.957442e+07   \n",
       "\n",
       "year               1992          1997          2002          2007  \n",
       "continent                                                          \n",
       "Africa     6.590815e+08  7.438330e+08  8.337239e+08  9.295397e+08  \n",
       "Americas   7.392741e+08  7.969004e+08  8.497728e+08  8.988712e+08  \n",
       "Asia       3.133292e+09  3.383286e+09  3.601802e+09  3.811954e+09  \n",
       "Europe     5.581428e+08  5.689441e+08  5.782239e+08  5.860985e+08  \n",
       "Oceania    2.091965e+07  2.224143e+07  2.345483e+07  2.454995e+07  "
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gap_data.pivot_table(index='continent',columns='year',values='pop', aggfunc=np.sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Adding margins for getting cumulative information\n",
    "* Margin names can also be added"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>year</th>\n",
       "      <th>1952</th>\n",
       "      <th>1957</th>\n",
       "      <th>1962</th>\n",
       "      <th>1967</th>\n",
       "      <th>1972</th>\n",
       "      <th>1977</th>\n",
       "      <th>1982</th>\n",
       "      <th>1987</th>\n",
       "      <th>1992</th>\n",
       "      <th>1997</th>\n",
       "      <th>2002</th>\n",
       "      <th>2007</th>\n",
       "      <th>Total</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>continent</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Africa</th>\n",
       "      <td>2.376405e+08</td>\n",
       "      <td>2.648377e+08</td>\n",
       "      <td>2.965169e+08</td>\n",
       "      <td>3.352895e+08</td>\n",
       "      <td>3.798795e+08</td>\n",
       "      <td>4.330610e+08</td>\n",
       "      <td>4.993486e+08</td>\n",
       "      <td>5.748341e+08</td>\n",
       "      <td>6.590815e+08</td>\n",
       "      <td>7.438330e+08</td>\n",
       "      <td>8.337239e+08</td>\n",
       "      <td>9.295397e+08</td>\n",
       "      <td>6.187586e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Americas</th>\n",
       "      <td>3.451524e+08</td>\n",
       "      <td>3.869539e+08</td>\n",
       "      <td>4.332703e+08</td>\n",
       "      <td>4.807466e+08</td>\n",
       "      <td>5.293842e+08</td>\n",
       "      <td>5.780677e+08</td>\n",
       "      <td>6.302909e+08</td>\n",
       "      <td>6.827540e+08</td>\n",
       "      <td>7.392741e+08</td>\n",
       "      <td>7.969004e+08</td>\n",
       "      <td>8.497728e+08</td>\n",
       "      <td>8.988712e+08</td>\n",
       "      <td>7.351438e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Asia</th>\n",
       "      <td>1.395357e+09</td>\n",
       "      <td>1.562781e+09</td>\n",
       "      <td>1.696357e+09</td>\n",
       "      <td>1.905663e+09</td>\n",
       "      <td>2.150972e+09</td>\n",
       "      <td>2.384514e+09</td>\n",
       "      <td>2.610136e+09</td>\n",
       "      <td>2.871221e+09</td>\n",
       "      <td>3.133292e+09</td>\n",
       "      <td>3.383286e+09</td>\n",
       "      <td>3.601802e+09</td>\n",
       "      <td>3.811954e+09</td>\n",
       "      <td>3.050733e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Europe</th>\n",
       "      <td>4.181208e+08</td>\n",
       "      <td>4.378904e+08</td>\n",
       "      <td>4.603552e+08</td>\n",
       "      <td>4.811790e+08</td>\n",
       "      <td>5.006351e+08</td>\n",
       "      <td>5.171645e+08</td>\n",
       "      <td>5.312669e+08</td>\n",
       "      <td>5.430942e+08</td>\n",
       "      <td>5.581428e+08</td>\n",
       "      <td>5.689441e+08</td>\n",
       "      <td>5.782239e+08</td>\n",
       "      <td>5.860985e+08</td>\n",
       "      <td>6.181115e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oceania</th>\n",
       "      <td>1.068601e+07</td>\n",
       "      <td>1.194198e+07</td>\n",
       "      <td>1.328352e+07</td>\n",
       "      <td>1.460041e+07</td>\n",
       "      <td>1.610610e+07</td>\n",
       "      <td>1.723900e+07</td>\n",
       "      <td>1.839485e+07</td>\n",
       "      <td>1.957442e+07</td>\n",
       "      <td>2.091965e+07</td>\n",
       "      <td>2.224143e+07</td>\n",
       "      <td>2.345483e+07</td>\n",
       "      <td>2.454995e+07</td>\n",
       "      <td>2.129921e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Total</th>\n",
       "      <td>2.406957e+09</td>\n",
       "      <td>2.664405e+09</td>\n",
       "      <td>2.899783e+09</td>\n",
       "      <td>3.217478e+09</td>\n",
       "      <td>3.576977e+09</td>\n",
       "      <td>3.930046e+09</td>\n",
       "      <td>4.289437e+09</td>\n",
       "      <td>4.691477e+09</td>\n",
       "      <td>5.110710e+09</td>\n",
       "      <td>5.515204e+09</td>\n",
       "      <td>5.886978e+09</td>\n",
       "      <td>6.251013e+09</td>\n",
       "      <td>5.044047e+10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "year               1952          1957          1962          1967  \\\n",
       "continent                                                           \n",
       "Africa     2.376405e+08  2.648377e+08  2.965169e+08  3.352895e+08   \n",
       "Americas   3.451524e+08  3.869539e+08  4.332703e+08  4.807466e+08   \n",
       "Asia       1.395357e+09  1.562781e+09  1.696357e+09  1.905663e+09   \n",
       "Europe     4.181208e+08  4.378904e+08  4.603552e+08  4.811790e+08   \n",
       "Oceania    1.068601e+07  1.194198e+07  1.328352e+07  1.460041e+07   \n",
       "Total      2.406957e+09  2.664405e+09  2.899783e+09  3.217478e+09   \n",
       "\n",
       "year               1972          1977          1982          1987  \\\n",
       "continent                                                           \n",
       "Africa     3.798795e+08  4.330610e+08  4.993486e+08  5.748341e+08   \n",
       "Americas   5.293842e+08  5.780677e+08  6.302909e+08  6.827540e+08   \n",
       "Asia       2.150972e+09  2.384514e+09  2.610136e+09  2.871221e+09   \n",
       "Europe     5.006351e+08  5.171645e+08  5.312669e+08  5.430942e+08   \n",
       "Oceania    1.610610e+07  1.723900e+07  1.839485e+07  1.957442e+07   \n",
       "Total      3.576977e+09  3.930046e+09  4.289437e+09  4.691477e+09   \n",
       "\n",
       "year               1992          1997          2002          2007  \\\n",
       "continent                                                           \n",
       "Africa     6.590815e+08  7.438330e+08  8.337239e+08  9.295397e+08   \n",
       "Americas   7.392741e+08  7.969004e+08  8.497728e+08  8.988712e+08   \n",
       "Asia       3.133292e+09  3.383286e+09  3.601802e+09  3.811954e+09   \n",
       "Europe     5.581428e+08  5.689441e+08  5.782239e+08  5.860985e+08   \n",
       "Oceania    2.091965e+07  2.224143e+07  2.345483e+07  2.454995e+07   \n",
       "Total      5.110710e+09  5.515204e+09  5.886978e+09  6.251013e+09   \n",
       "\n",
       "year              Total  \n",
       "continent                \n",
       "Africa     6.187586e+09  \n",
       "Americas   7.351438e+09  \n",
       "Asia       3.050733e+10  \n",
       "Europe     6.181115e+09  \n",
       "Oceania    2.129921e+08  \n",
       "Total      5.044047e+10  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gap_data.pivot_table(index='continent',columns='year',values='pop', aggfunc=np.sum,margins=True, margins_name='Total')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Stacking & Unstacking\n",
    "* Let us assume we have a DataFrame with MultiIndices on the rows and columns. \n",
    "* Stacking a DataFrame means moving (also rotating or pivoting) the innermost column index to become the innermost row index. \n",
    "* The inverse operation is called unstacking. It means moving the innermost row index to become the innermost column index. \n",
    "* Stacking makes dataframe taller & can yield useful insights.\n",
    "* Unstacking makes dataframe wider & can yield useful observations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Mumbai</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Delhi</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Bangalore</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>two-wheeler</th>\n",
       "      <th>four-wheeler</th>\n",
       "      <th>two-wheeler</th>\n",
       "      <th>four-wheeler</th>\n",
       "      <th>two-wheeler</th>\n",
       "      <th>four-wheeler</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>death</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013</th>\n",
       "      <th>yes</th>\n",
       "      <td>24</td>\n",
       "      <td>38</td>\n",
       "      <td>89</td>\n",
       "      <td>72</td>\n",
       "      <td>20</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>no</th>\n",
       "      <td>29</td>\n",
       "      <td>72</td>\n",
       "      <td>43</td>\n",
       "      <td>15</td>\n",
       "      <td>95</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2014</th>\n",
       "      <th>yes</th>\n",
       "      <td>8</td>\n",
       "      <td>40</td>\n",
       "      <td>82</td>\n",
       "      <td>8</td>\n",
       "      <td>22</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>no</th>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>98</td>\n",
       "      <td>32</td>\n",
       "      <td>12</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "city            Mumbai                    Delhi                Bangalore  \\\n",
       "type       two-wheeler four-wheeler two-wheeler four-wheeler two-wheeler   \n",
       "year death                                                                 \n",
       "2013 yes            24           38          89           72          20   \n",
       "     no             29           72          43           15          95   \n",
       "2014 yes             8           40          82            8          22   \n",
       "     no              1           19          98           32          12   \n",
       "\n",
       "city                     \n",
       "type       four-wheeler  \n",
       "year death               \n",
       "2013 yes             19  \n",
       "     no              17  \n",
       "2014 yes             73  \n",
       "     no              62  "
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = pd.MultiIndex.from_product([[2013, 2014], ['yes','no']],\n",
    "                                   names=['year', 'death'])\n",
    "columns = pd.MultiIndex.from_product([['Mumbai', 'Delhi', 'Bangalore'], \n",
    "                                      ['two-wheeler', 'four-wheeler']],\n",
    "                                     names=['city', 'type'])\n",
    "\n",
    "\n",
    "data = np.random.randint(1,100,(4,6))\n",
    "\n",
    "accident_data = pd.DataFrame(data, index=index, columns=columns)\n",
    "accident_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "      <th>Bangalore</th>\n",
       "      <th>Delhi</th>\n",
       "      <th>Mumbai</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>death</th>\n",
       "      <th>type</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">2013</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">yes</th>\n",
       "      <th>four-wheeler</th>\n",
       "      <td>19</td>\n",
       "      <td>72</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two-wheeler</th>\n",
       "      <td>20</td>\n",
       "      <td>89</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">no</th>\n",
       "      <th>four-wheeler</th>\n",
       "      <td>17</td>\n",
       "      <td>15</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two-wheeler</th>\n",
       "      <td>95</td>\n",
       "      <td>43</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">2014</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">yes</th>\n",
       "      <th>four-wheeler</th>\n",
       "      <td>73</td>\n",
       "      <td>8</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two-wheeler</th>\n",
       "      <td>22</td>\n",
       "      <td>82</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">no</th>\n",
       "      <th>four-wheeler</th>\n",
       "      <td>62</td>\n",
       "      <td>32</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two-wheeler</th>\n",
       "      <td>12</td>\n",
       "      <td>98</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "city                     Bangalore  Delhi  Mumbai\n",
       "year death type                                  \n",
       "2013 yes   four-wheeler         19     72      38\n",
       "           two-wheeler          20     89      24\n",
       "     no    four-wheeler         17     15      72\n",
       "           two-wheeler          95     43      29\n",
       "2014 yes   four-wheeler         73      8      40\n",
       "           two-wheeler          22     82       8\n",
       "     no    four-wheeler         62     32      19\n",
       "           two-wheeler          12     98       1"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accident_data.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th colspan=\"4\" halign=\"left\">Mumbai</th>\n",
       "      <th colspan=\"4\" halign=\"left\">Delhi</th>\n",
       "      <th colspan=\"4\" halign=\"left\">Bangalore</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>type</th>\n",
       "      <th colspan=\"2\" halign=\"left\">two-wheeler</th>\n",
       "      <th colspan=\"2\" halign=\"left\">four-wheeler</th>\n",
       "      <th colspan=\"2\" halign=\"left\">two-wheeler</th>\n",
       "      <th colspan=\"2\" halign=\"left\">four-wheeler</th>\n",
       "      <th colspan=\"2\" halign=\"left\">two-wheeler</th>\n",
       "      <th colspan=\"2\" halign=\"left\">four-wheeler</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>death</th>\n",
       "      <th>no</th>\n",
       "      <th>yes</th>\n",
       "      <th>no</th>\n",
       "      <th>yes</th>\n",
       "      <th>no</th>\n",
       "      <th>yes</th>\n",
       "      <th>no</th>\n",
       "      <th>yes</th>\n",
       "      <th>no</th>\n",
       "      <th>yes</th>\n",
       "      <th>no</th>\n",
       "      <th>yes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013</th>\n",
       "      <td>29</td>\n",
       "      <td>24</td>\n",
       "      <td>72</td>\n",
       "      <td>38</td>\n",
       "      <td>43</td>\n",
       "      <td>89</td>\n",
       "      <td>15</td>\n",
       "      <td>72</td>\n",
       "      <td>95</td>\n",
       "      <td>20</td>\n",
       "      <td>17</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>19</td>\n",
       "      <td>40</td>\n",
       "      <td>98</td>\n",
       "      <td>82</td>\n",
       "      <td>32</td>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "      <td>22</td>\n",
       "      <td>62</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "city       Mumbai                            Delhi                       \\\n",
       "type  two-wheeler     four-wheeler     two-wheeler     four-wheeler       \n",
       "death          no yes           no yes          no yes           no yes   \n",
       "year                                                                      \n",
       "2013           29  24           72  38          43  89           15  72   \n",
       "2014            1   8           19  40          98  82           32   8   \n",
       "\n",
       "city    Bangalore                       \n",
       "type  two-wheeler     four-wheeler      \n",
       "death          no yes           no yes  \n",
       "year                                    \n",
       "2013           95  20           17  19  \n",
       "2014           12  22           62  73  "
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accident_data.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "countries = ['India','India','US','US','Australia','Australia','Japan','Japan']\n",
    "gender = ['male','female','male','female','male','female','male','female']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('India', 'male'),\n",
       " ('India', 'female'),\n",
       " ('US', 'male'),\n",
       " ('US', 'female'),\n",
       " ('Australia', 'male'),\n",
       " ('Australia', 'female'),\n",
       " ('Japan', 'male'),\n",
       " ('Japan', 'female')]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(countries,gender))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pd.MultiIndex.from_tuples(list(zip(countries,gender)), names=['country', 'gender'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['Australia', 'India', 'Japan', 'US'], ['female', 'male']],\n",
       "           codes=[[1, 1, 3, 3, 0, 0, 2, 2], [1, 0, 1, 0, 1, 0, 1, 0]],\n",
       "           names=['country', 'gender'])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "fake_phd_data = pd.DataFrame([10,20,13,15,16,20,33,12], index=index, columns=['PhDs'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>PhDs</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>country</th>\n",
       "      <th>gender</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">India</th>\n",
       "      <th>male</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>female</th>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">US</th>\n",
       "      <th>male</th>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>female</th>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Australia</th>\n",
       "      <th>male</th>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>female</th>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Japan</th>\n",
       "      <th>male</th>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>female</th>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  PhDs\n",
       "country   gender      \n",
       "India     male      10\n",
       "          female    20\n",
       "US        male      13\n",
       "          female    15\n",
       "Australia male      16\n",
       "          female    20\n",
       "Japan     male      33\n",
       "          female    12"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fake_phd_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">PhDs</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>gender</th>\n",
       "      <th>female</th>\n",
       "      <th>male</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>country</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Australia</th>\n",
       "      <td>20</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>India</th>\n",
       "      <td>20</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Japan</th>\n",
       "      <td>12</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>US</th>\n",
       "      <td>15</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            PhDs     \n",
       "gender    female male\n",
       "country              \n",
       "Australia     20   16\n",
       "India         20   10\n",
       "Japan         12   33\n",
       "US            15   13"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fake_phd_data.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>country</th>\n",
       "      <th>Australia</th>\n",
       "      <th>India</th>\n",
       "      <th>Japan</th>\n",
       "      <th>US</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>gender</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">PhDs</th>\n",
       "      <th>female</th>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>12</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>male</th>\n",
       "      <td>16</td>\n",
       "      <td>10</td>\n",
       "      <td>33</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "country      Australia  India  Japan  US\n",
       "     gender                             \n",
       "PhDs female         20     20     12  15\n",
       "     male           16     10     33  13"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fake_phd_data.T.stack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Melting\n",
    "* Unpivot a DataFrame from wide format to long format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a pivot table first\n",
    "res = gap_data.pivot_table(index='continent',columns='year',values='gdpPercap', aggfunc=np.sum).round(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>year</th>\n",
       "      <th>1952</th>\n",
       "      <th>1957</th>\n",
       "      <th>1962</th>\n",
       "      <th>1967</th>\n",
       "      <th>1972</th>\n",
       "      <th>1977</th>\n",
       "      <th>1982</th>\n",
       "      <th>1987</th>\n",
       "      <th>1992</th>\n",
       "      <th>1997</th>\n",
       "      <th>2002</th>\n",
       "      <th>2007</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>continent</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Africa</th>\n",
       "      <td>65133.8</td>\n",
       "      <td>72032.3</td>\n",
       "      <td>83100.1</td>\n",
       "      <td>106618.9</td>\n",
       "      <td>121660.0</td>\n",
       "      <td>134468.8</td>\n",
       "      <td>129042.8</td>\n",
       "      <td>118698.8</td>\n",
       "      <td>118654.1</td>\n",
       "      <td>123695.5</td>\n",
       "      <td>135168.0</td>\n",
       "      <td>160629.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Americas</th>\n",
       "      <td>101976.6</td>\n",
       "      <td>115401.1</td>\n",
       "      <td>122538.5</td>\n",
       "      <td>141706.3</td>\n",
       "      <td>162283.4</td>\n",
       "      <td>183800.2</td>\n",
       "      <td>187668.4</td>\n",
       "      <td>194835.0</td>\n",
       "      <td>201123.4</td>\n",
       "      <td>222232.5</td>\n",
       "      <td>232191.9</td>\n",
       "      <td>275075.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Asia</th>\n",
       "      <td>171451.0</td>\n",
       "      <td>190995.2</td>\n",
       "      <td>189069.2</td>\n",
       "      <td>197048.7</td>\n",
       "      <td>270186.5</td>\n",
       "      <td>257113.4</td>\n",
       "      <td>245326.5</td>\n",
       "      <td>251071.5</td>\n",
       "      <td>285109.8</td>\n",
       "      <td>324525.1</td>\n",
       "      <td>335745.0</td>\n",
       "      <td>411609.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Europe</th>\n",
       "      <td>169831.7</td>\n",
       "      <td>208890.4</td>\n",
       "      <td>250964.6</td>\n",
       "      <td>304314.7</td>\n",
       "      <td>374387.3</td>\n",
       "      <td>428519.4</td>\n",
       "      <td>468536.9</td>\n",
       "      <td>516429.3</td>\n",
       "      <td>511847.0</td>\n",
       "      <td>572303.5</td>\n",
       "      <td>651352.0</td>\n",
       "      <td>751634.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oceania</th>\n",
       "      <td>20596.2</td>\n",
       "      <td>23197.0</td>\n",
       "      <td>25392.9</td>\n",
       "      <td>28990.0</td>\n",
       "      <td>32834.7</td>\n",
       "      <td>34567.9</td>\n",
       "      <td>37109.4</td>\n",
       "      <td>40896.1</td>\n",
       "      <td>41788.1</td>\n",
       "      <td>48048.4</td>\n",
       "      <td>53877.6</td>\n",
       "      <td>59620.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "year           1952      1957      1962      1967      1972      1977  \\\n",
       "continent                                                               \n",
       "Africa      65133.8   72032.3   83100.1  106618.9  121660.0  134468.8   \n",
       "Americas   101976.6  115401.1  122538.5  141706.3  162283.4  183800.2   \n",
       "Asia       171451.0  190995.2  189069.2  197048.7  270186.5  257113.4   \n",
       "Europe     169831.7  208890.4  250964.6  304314.7  374387.3  428519.4   \n",
       "Oceania     20596.2   23197.0   25392.9   28990.0   32834.7   34567.9   \n",
       "\n",
       "year           1982      1987      1992      1997      2002      2007  \n",
       "continent                                                              \n",
       "Africa     129042.8  118698.8  118654.1  123695.5  135168.0  160629.7  \n",
       "Americas   187668.4  194835.0  201123.4  222232.5  232191.9  275075.8  \n",
       "Asia       245326.5  251071.5  285109.8  324525.1  335745.0  411609.9  \n",
       "Europe     468536.9  516429.3  511847.0  572303.5  651352.0  751634.4  \n",
       "Oceania     37109.4   40896.1   41788.1   48048.4   53877.6   59620.4  "
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [],
   "source": [
    "res.reset_index(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>year</th>\n",
       "      <th>continent</th>\n",
       "      <th>1952</th>\n",
       "      <th>1957</th>\n",
       "      <th>1962</th>\n",
       "      <th>1967</th>\n",
       "      <th>1972</th>\n",
       "      <th>1977</th>\n",
       "      <th>1982</th>\n",
       "      <th>1987</th>\n",
       "      <th>1992</th>\n",
       "      <th>1997</th>\n",
       "      <th>2002</th>\n",
       "      <th>2007</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Africa</td>\n",
       "      <td>65133.8</td>\n",
       "      <td>72032.3</td>\n",
       "      <td>83100.1</td>\n",
       "      <td>106618.9</td>\n",
       "      <td>121660.0</td>\n",
       "      <td>134468.8</td>\n",
       "      <td>129042.8</td>\n",
       "      <td>118698.8</td>\n",
       "      <td>118654.1</td>\n",
       "      <td>123695.5</td>\n",
       "      <td>135168.0</td>\n",
       "      <td>160629.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Americas</td>\n",
       "      <td>101976.6</td>\n",
       "      <td>115401.1</td>\n",
       "      <td>122538.5</td>\n",
       "      <td>141706.3</td>\n",
       "      <td>162283.4</td>\n",
       "      <td>183800.2</td>\n",
       "      <td>187668.4</td>\n",
       "      <td>194835.0</td>\n",
       "      <td>201123.4</td>\n",
       "      <td>222232.5</td>\n",
       "      <td>232191.9</td>\n",
       "      <td>275075.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Asia</td>\n",
       "      <td>171451.0</td>\n",
       "      <td>190995.2</td>\n",
       "      <td>189069.2</td>\n",
       "      <td>197048.7</td>\n",
       "      <td>270186.5</td>\n",
       "      <td>257113.4</td>\n",
       "      <td>245326.5</td>\n",
       "      <td>251071.5</td>\n",
       "      <td>285109.8</td>\n",
       "      <td>324525.1</td>\n",
       "      <td>335745.0</td>\n",
       "      <td>411609.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Europe</td>\n",
       "      <td>169831.7</td>\n",
       "      <td>208890.4</td>\n",
       "      <td>250964.6</td>\n",
       "      <td>304314.7</td>\n",
       "      <td>374387.3</td>\n",
       "      <td>428519.4</td>\n",
       "      <td>468536.9</td>\n",
       "      <td>516429.3</td>\n",
       "      <td>511847.0</td>\n",
       "      <td>572303.5</td>\n",
       "      <td>651352.0</td>\n",
       "      <td>751634.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>20596.2</td>\n",
       "      <td>23197.0</td>\n",
       "      <td>25392.9</td>\n",
       "      <td>28990.0</td>\n",
       "      <td>32834.7</td>\n",
       "      <td>34567.9</td>\n",
       "      <td>37109.4</td>\n",
       "      <td>40896.1</td>\n",
       "      <td>41788.1</td>\n",
       "      <td>48048.4</td>\n",
       "      <td>53877.6</td>\n",
       "      <td>59620.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "year continent      1952      1957      1962      1967      1972      1977  \\\n",
       "0       Africa   65133.8   72032.3   83100.1  106618.9  121660.0  134468.8   \n",
       "1     Americas  101976.6  115401.1  122538.5  141706.3  162283.4  183800.2   \n",
       "2         Asia  171451.0  190995.2  189069.2  197048.7  270186.5  257113.4   \n",
       "3       Europe  169831.7  208890.4  250964.6  304314.7  374387.3  428519.4   \n",
       "4      Oceania   20596.2   23197.0   25392.9   28990.0   32834.7   34567.9   \n",
       "\n",
       "year      1982      1987      1992      1997      2002      2007  \n",
       "0     129042.8  118698.8  118654.1  123695.5  135168.0  160629.7  \n",
       "1     187668.4  194835.0  201123.4  222232.5  232191.9  275075.8  \n",
       "2     245326.5  251071.5  285109.8  324525.1  335745.0  411609.9  \n",
       "3     468536.9  516429.3  511847.0  572303.5  651352.0  751634.4  \n",
       "4      37109.4   40896.1   41788.1   48048.4   53877.6   59620.4  "
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>continent</th>\n",
       "      <th>year</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1952</td>\n",
       "      <td>65133.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1952</td>\n",
       "      <td>101976.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1952</td>\n",
       "      <td>171451.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1952</td>\n",
       "      <td>169831.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1952</td>\n",
       "      <td>20596.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1957</td>\n",
       "      <td>72032.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1957</td>\n",
       "      <td>115401.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1957</td>\n",
       "      <td>190995.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1957</td>\n",
       "      <td>208890.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1957</td>\n",
       "      <td>23197.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1962</td>\n",
       "      <td>83100.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1962</td>\n",
       "      <td>122538.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1962</td>\n",
       "      <td>189069.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1962</td>\n",
       "      <td>250964.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1962</td>\n",
       "      <td>25392.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1967</td>\n",
       "      <td>106618.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1967</td>\n",
       "      <td>141706.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1967</td>\n",
       "      <td>197048.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1967</td>\n",
       "      <td>304314.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1967</td>\n",
       "      <td>28990.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1972</td>\n",
       "      <td>121660.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1972</td>\n",
       "      <td>162283.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1972</td>\n",
       "      <td>270186.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1972</td>\n",
       "      <td>374387.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1972</td>\n",
       "      <td>32834.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1977</td>\n",
       "      <td>134468.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1977</td>\n",
       "      <td>183800.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1977</td>\n",
       "      <td>257113.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1977</td>\n",
       "      <td>428519.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1977</td>\n",
       "      <td>34567.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1982</td>\n",
       "      <td>129042.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1982</td>\n",
       "      <td>187668.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1982</td>\n",
       "      <td>245326.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1982</td>\n",
       "      <td>468536.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1982</td>\n",
       "      <td>37109.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1987</td>\n",
       "      <td>118698.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1987</td>\n",
       "      <td>194835.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1987</td>\n",
       "      <td>251071.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1987</td>\n",
       "      <td>516429.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1987</td>\n",
       "      <td>40896.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1992</td>\n",
       "      <td>118654.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1992</td>\n",
       "      <td>201123.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1992</td>\n",
       "      <td>285109.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1992</td>\n",
       "      <td>511847.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1992</td>\n",
       "      <td>41788.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1997</td>\n",
       "      <td>123695.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1997</td>\n",
       "      <td>222232.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1997</td>\n",
       "      <td>324525.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1997</td>\n",
       "      <td>572303.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1997</td>\n",
       "      <td>48048.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>Africa</td>\n",
       "      <td>2002</td>\n",
       "      <td>135168.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>Americas</td>\n",
       "      <td>2002</td>\n",
       "      <td>232191.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>Asia</td>\n",
       "      <td>2002</td>\n",
       "      <td>335745.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>Europe</td>\n",
       "      <td>2002</td>\n",
       "      <td>651352.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>2002</td>\n",
       "      <td>53877.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>Africa</td>\n",
       "      <td>2007</td>\n",
       "      <td>160629.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>Americas</td>\n",
       "      <td>2007</td>\n",
       "      <td>275075.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>Asia</td>\n",
       "      <td>2007</td>\n",
       "      <td>411609.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>Europe</td>\n",
       "      <td>2007</td>\n",
       "      <td>751634.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>2007</td>\n",
       "      <td>59620.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   continent  year     value\n",
       "0     Africa  1952   65133.8\n",
       "1   Americas  1952  101976.6\n",
       "2       Asia  1952  171451.0\n",
       "3     Europe  1952  169831.7\n",
       "4    Oceania  1952   20596.2\n",
       "5     Africa  1957   72032.3\n",
       "6   Americas  1957  115401.1\n",
       "7       Asia  1957  190995.2\n",
       "8     Europe  1957  208890.4\n",
       "9    Oceania  1957   23197.0\n",
       "10    Africa  1962   83100.1\n",
       "11  Americas  1962  122538.5\n",
       "12      Asia  1962  189069.2\n",
       "13    Europe  1962  250964.6\n",
       "14   Oceania  1962   25392.9\n",
       "15    Africa  1967  106618.9\n",
       "16  Americas  1967  141706.3\n",
       "17      Asia  1967  197048.7\n",
       "18    Europe  1967  304314.7\n",
       "19   Oceania  1967   28990.0\n",
       "20    Africa  1972  121660.0\n",
       "21  Americas  1972  162283.4\n",
       "22      Asia  1972  270186.5\n",
       "23    Europe  1972  374387.3\n",
       "24   Oceania  1972   32834.7\n",
       "25    Africa  1977  134468.8\n",
       "26  Americas  1977  183800.2\n",
       "27      Asia  1977  257113.4\n",
       "28    Europe  1977  428519.4\n",
       "29   Oceania  1977   34567.9\n",
       "30    Africa  1982  129042.8\n",
       "31  Americas  1982  187668.4\n",
       "32      Asia  1982  245326.5\n",
       "33    Europe  1982  468536.9\n",
       "34   Oceania  1982   37109.4\n",
       "35    Africa  1987  118698.8\n",
       "36  Americas  1987  194835.0\n",
       "37      Asia  1987  251071.5\n",
       "38    Europe  1987  516429.3\n",
       "39   Oceania  1987   40896.1\n",
       "40    Africa  1992  118654.1\n",
       "41  Americas  1992  201123.4\n",
       "42      Asia  1992  285109.8\n",
       "43    Europe  1992  511847.0\n",
       "44   Oceania  1992   41788.1\n",
       "45    Africa  1997  123695.5\n",
       "46  Americas  1997  222232.5\n",
       "47      Asia  1997  324525.1\n",
       "48    Europe  1997  572303.5\n",
       "49   Oceania  1997   48048.4\n",
       "50    Africa  2002  135168.0\n",
       "51  Americas  2002  232191.9\n",
       "52      Asia  2002  335745.0\n",
       "53    Europe  2002  651352.0\n",
       "54   Oceania  2002   53877.6\n",
       "55    Africa  2007  160629.7\n",
       "56  Americas  2007  275075.8\n",
       "57      Asia  2007  411609.9\n",
       "58    Europe  2007  751634.4\n",
       "59   Oceania  2007   59620.4"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "melted_df = pd.melt(res, id_vars=['continent'])\n",
    "melted_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>continent</th>\n",
       "      <th>year</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1952</td>\n",
       "      <td>65133.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1957</td>\n",
       "      <td>72032.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1962</td>\n",
       "      <td>83100.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1967</td>\n",
       "      <td>106618.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1972</td>\n",
       "      <td>121660.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1977</td>\n",
       "      <td>134468.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1982</td>\n",
       "      <td>129042.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1987</td>\n",
       "      <td>118698.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1992</td>\n",
       "      <td>118654.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>Africa</td>\n",
       "      <td>1997</td>\n",
       "      <td>123695.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>Africa</td>\n",
       "      <td>2002</td>\n",
       "      <td>135168.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>Africa</td>\n",
       "      <td>2007</td>\n",
       "      <td>160629.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1952</td>\n",
       "      <td>101976.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1957</td>\n",
       "      <td>115401.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1962</td>\n",
       "      <td>122538.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1967</td>\n",
       "      <td>141706.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1972</td>\n",
       "      <td>162283.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1977</td>\n",
       "      <td>183800.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1982</td>\n",
       "      <td>187668.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1987</td>\n",
       "      <td>194835.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1992</td>\n",
       "      <td>201123.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>Americas</td>\n",
       "      <td>1997</td>\n",
       "      <td>222232.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>Americas</td>\n",
       "      <td>2002</td>\n",
       "      <td>232191.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>Americas</td>\n",
       "      <td>2007</td>\n",
       "      <td>275075.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1952</td>\n",
       "      <td>171451.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1957</td>\n",
       "      <td>190995.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1962</td>\n",
       "      <td>189069.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1967</td>\n",
       "      <td>197048.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1972</td>\n",
       "      <td>270186.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1977</td>\n",
       "      <td>257113.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1982</td>\n",
       "      <td>245326.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1987</td>\n",
       "      <td>251071.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1992</td>\n",
       "      <td>285109.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>Asia</td>\n",
       "      <td>1997</td>\n",
       "      <td>324525.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>Asia</td>\n",
       "      <td>2002</td>\n",
       "      <td>335745.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>Asia</td>\n",
       "      <td>2007</td>\n",
       "      <td>411609.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1952</td>\n",
       "      <td>169831.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1957</td>\n",
       "      <td>208890.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1962</td>\n",
       "      <td>250964.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1967</td>\n",
       "      <td>304314.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1972</td>\n",
       "      <td>374387.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1977</td>\n",
       "      <td>428519.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1982</td>\n",
       "      <td>468536.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1987</td>\n",
       "      <td>516429.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1992</td>\n",
       "      <td>511847.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>Europe</td>\n",
       "      <td>1997</td>\n",
       "      <td>572303.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>Europe</td>\n",
       "      <td>2002</td>\n",
       "      <td>651352.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>Europe</td>\n",
       "      <td>2007</td>\n",
       "      <td>751634.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1952</td>\n",
       "      <td>20596.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1957</td>\n",
       "      <td>23197.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1962</td>\n",
       "      <td>25392.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1967</td>\n",
       "      <td>28990.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1972</td>\n",
       "      <td>32834.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1977</td>\n",
       "      <td>34567.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1982</td>\n",
       "      <td>37109.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1987</td>\n",
       "      <td>40896.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1992</td>\n",
       "      <td>41788.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>1997</td>\n",
       "      <td>48048.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>2002</td>\n",
       "      <td>53877.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>Oceania</td>\n",
       "      <td>2007</td>\n",
       "      <td>59620.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   continent  year     value\n",
       "0     Africa  1952   65133.8\n",
       "5     Africa  1957   72032.3\n",
       "10    Africa  1962   83100.1\n",
       "15    Africa  1967  106618.9\n",
       "20    Africa  1972  121660.0\n",
       "25    Africa  1977  134468.8\n",
       "30    Africa  1982  129042.8\n",
       "35    Africa  1987  118698.8\n",
       "40    Africa  1992  118654.1\n",
       "45    Africa  1997  123695.5\n",
       "50    Africa  2002  135168.0\n",
       "55    Africa  2007  160629.7\n",
       "1   Americas  1952  101976.6\n",
       "6   Americas  1957  115401.1\n",
       "11  Americas  1962  122538.5\n",
       "16  Americas  1967  141706.3\n",
       "21  Americas  1972  162283.4\n",
       "26  Americas  1977  183800.2\n",
       "31  Americas  1982  187668.4\n",
       "36  Americas  1987  194835.0\n",
       "41  Americas  1992  201123.4\n",
       "46  Americas  1997  222232.5\n",
       "51  Americas  2002  232191.9\n",
       "56  Americas  2007  275075.8\n",
       "2       Asia  1952  171451.0\n",
       "7       Asia  1957  190995.2\n",
       "12      Asia  1962  189069.2\n",
       "17      Asia  1967  197048.7\n",
       "22      Asia  1972  270186.5\n",
       "27      Asia  1977  257113.4\n",
       "32      Asia  1982  245326.5\n",
       "37      Asia  1987  251071.5\n",
       "42      Asia  1992  285109.8\n",
       "47      Asia  1997  324525.1\n",
       "52      Asia  2002  335745.0\n",
       "57      Asia  2007  411609.9\n",
       "3     Europe  1952  169831.7\n",
       "8     Europe  1957  208890.4\n",
       "13    Europe  1962  250964.6\n",
       "18    Europe  1967  304314.7\n",
       "23    Europe  1972  374387.3\n",
       "28    Europe  1977  428519.4\n",
       "33    Europe  1982  468536.9\n",
       "38    Europe  1987  516429.3\n",
       "43    Europe  1992  511847.0\n",
       "48    Europe  1997  572303.5\n",
       "53    Europe  2002  651352.0\n",
       "58    Europe  2007  751634.4\n",
       "4    Oceania  1952   20596.2\n",
       "9    Oceania  1957   23197.0\n",
       "14   Oceania  1962   25392.9\n",
       "19   Oceania  1967   28990.0\n",
       "24   Oceania  1972   32834.7\n",
       "29   Oceania  1977   34567.9\n",
       "34   Oceania  1982   37109.4\n",
       "39   Oceania  1987   40896.1\n",
       "44   Oceania  1992   41788.1\n",
       "49   Oceania  1997   48048.4\n",
       "54   Oceania  2002   53877.6\n",
       "59   Oceania  2007   59620.4"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "melted_df.sort_values(['continent','year']).round(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. GroupBy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data = pd.read_csv('../Data/titanic-train.csv.txt', index_col='PassengerId')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pclass  Sex     Survived\n",
       "1       female  0             3\n",
       "                1            91\n",
       "        male    0            77\n",
       "                1            45\n",
       "2       female  0             6\n",
       "                1            70\n",
       "        male    0            91\n",
       "                1            17\n",
       "3       female  0            72\n",
       "                1            72\n",
       "        male    0           300\n",
       "                1            47\n",
       "dtype: int64"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic_data.groupby(['Pclass','Sex','Survived']).size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "continent\n",
       "Africa      48.865330\n",
       "Americas    64.658737\n",
       "Asia        60.064903\n",
       "Europe      71.903686\n",
       "Oceania     74.326208\n",
       "Name: lifeExp, dtype: float64"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gap_data.groupby(['continent']).lifeExp.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "continent  country                 \n",
       "Africa     Sierra Leone                36.8\n",
       "Asia       Afghanistan                 37.5\n",
       "Africa     Angola                      37.9\n",
       "           Guinea-Bissau               39.2\n",
       "           Mozambique                  40.4\n",
       "           Somalia                     41.0\n",
       "           Rwanda                      41.5\n",
       "           Liberia                     42.5\n",
       "           Equatorial Guinea           43.0\n",
       "           Guinea                      43.2\n",
       "           Malawi                      43.4\n",
       "           Mali                        43.4\n",
       "           Nigeria                     43.6\n",
       "           Central African Republic    43.9\n",
       "           Gambia                      44.4\n",
       "           Ethiopia                    44.5\n",
       "           Congo Dem. Rep.             44.5\n",
       "           Niger                       44.6\n",
       "           Burkina Faso                44.7\n",
       "           Burundi                     44.8\n",
       "           Eritrea                     46.0\n",
       "           Zambia                      46.0\n",
       "           Djibouti                    46.4\n",
       "           Chad                        46.8\n",
       "Asia       Yemen Rep.                  46.8\n",
       "Africa     Uganda                      47.6\n",
       "           Madagascar                  47.8\n",
       "Asia       Cambodia                    47.9\n",
       "Africa     Tanzania                    47.9\n",
       "           Cameroon                    48.1\n",
       "                                       ... \n",
       "Europe     Slovak Republic             70.7\n",
       "Americas   Uruguay                     70.8\n",
       "           Cuba                        71.0\n",
       "Asia       Singapore                   71.2\n",
       "Europe     Czech Republic              71.5\n",
       "           Slovenia                    71.6\n",
       "Americas   Puerto Rico                 72.7\n",
       "Europe     Finland                     73.0\n",
       "           Ireland                     73.0\n",
       "           Austria                     73.1\n",
       "           Germany                     73.4\n",
       "Asia       Hong Kong China             73.5\n",
       "Americas   United States               73.5\n",
       "Asia       Israel                      73.6\n",
       "Europe     Belgium                     73.6\n",
       "           Greece                      73.7\n",
       "           United Kingdom              73.9\n",
       "           Italy                       74.0\n",
       "Oceania    New Zealand                 74.0\n",
       "Europe     Spain                       74.2\n",
       "           France                      74.3\n",
       "           Denmark                     74.4\n",
       "Oceania    Australia                   74.7\n",
       "Asia       Japan                       74.8\n",
       "Americas   Canada                      74.9\n",
       "Europe     Netherlands                 75.6\n",
       "           Switzerland                 75.6\n",
       "           Norway                      75.8\n",
       "           Sweden                      76.2\n",
       "           Iceland                     76.5\n",
       "Name: lifeExp, Length: 142, dtype: float64"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gap_data.groupby(['continent','country']).lifeExp.mean().round(1).sort_values()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. Cross Tabulations\n",
    "* CrossTab() is used to compute a cross-tabulation of two (or more) factors. By default crosstab computes a frequency table of the factors unless an array of values and an aggregation function are passed.\n",
    "* CrossTab is one of the easiest way to get quick results compared to pivot_table & other options\n",
    "* The question still remains, why even use a crosstab function? The short answer is that it provides a couple of handy functions to more easily format and summarize the data.\n",
    "* The longer answer is that sometimes it can be tough to remember all the steps to make this happen on your own. The simple crosstab API is the quickest route to the solution and provides some useful shortcuts for certain types of analysis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = [\"symboling\", \"normalized_losses\", \"make\", \"fuel_type\", \"aspiration\",\n",
    "           \"num_doors\", \"body_style\", \"drive_wheels\", \"engine_location\",\n",
    "           \"wheel_base\", \"length\", \"width\", \"height\", \"curb_weight\",\n",
    "           \"engine_type\", \"num_cylinders\", \"engine_size\", \"fuel_system\",\n",
    "           \"bore\", \"stroke\", \"compression_ratio\", \"horsepower\", \"peak_rpm\",\n",
    "           \"city_mpg\", \"highway_mpg\", \"price\"]\n",
    "\n",
    "# Read in the CSV file and convert \"?\" to NaN\n",
    "df_raw = pd.read_csv(\"http://mlr.cs.umass.edu/ml/machine-learning-databases/autos/imports-85.data\",\n",
    "                     header=None, names=headers, na_values=\"?\" )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symboling</th>\n",
       "      <th>normalized_losses</th>\n",
       "      <th>make</th>\n",
       "      <th>fuel_type</th>\n",
       "      <th>aspiration</th>\n",
       "      <th>num_doors</th>\n",
       "      <th>body_style</th>\n",
       "      <th>drive_wheels</th>\n",
       "      <th>engine_location</th>\n",
       "      <th>wheel_base</th>\n",
       "      <th>...</th>\n",
       "      <th>engine_size</th>\n",
       "      <th>fuel_system</th>\n",
       "      <th>bore</th>\n",
       "      <th>stroke</th>\n",
       "      <th>compression_ratio</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>peak_rpm</th>\n",
       "      <th>city_mpg</th>\n",
       "      <th>highway_mpg</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>alfa-romero</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>convertible</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>88.6</td>\n",
       "      <td>...</td>\n",
       "      <td>130</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.47</td>\n",
       "      <td>2.68</td>\n",
       "      <td>9.00</td>\n",
       "      <td>111.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>21</td>\n",
       "      <td>27</td>\n",
       "      <td>13495.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>alfa-romero</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>convertible</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>88.6</td>\n",
       "      <td>...</td>\n",
       "      <td>130</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.47</td>\n",
       "      <td>2.68</td>\n",
       "      <td>9.00</td>\n",
       "      <td>111.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>21</td>\n",
       "      <td>27</td>\n",
       "      <td>16500.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>alfa-romero</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>94.5</td>\n",
       "      <td>...</td>\n",
       "      <td>152</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>2.68</td>\n",
       "      <td>3.47</td>\n",
       "      <td>9.00</td>\n",
       "      <td>154.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>19</td>\n",
       "      <td>26</td>\n",
       "      <td>16500.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>164.0</td>\n",
       "      <td>audi</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>99.8</td>\n",
       "      <td>...</td>\n",
       "      <td>109</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>10.00</td>\n",
       "      <td>102.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>24</td>\n",
       "      <td>30</td>\n",
       "      <td>13950.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>164.0</td>\n",
       "      <td>audi</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>4wd</td>\n",
       "      <td>front</td>\n",
       "      <td>99.4</td>\n",
       "      <td>...</td>\n",
       "      <td>136</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>8.00</td>\n",
       "      <td>115.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>18</td>\n",
       "      <td>22</td>\n",
       "      <td>17450.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>audi</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>99.8</td>\n",
       "      <td>...</td>\n",
       "      <td>136</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>8.50</td>\n",
       "      <td>110.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>19</td>\n",
       "      <td>25</td>\n",
       "      <td>15250.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>158.0</td>\n",
       "      <td>audi</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>105.8</td>\n",
       "      <td>...</td>\n",
       "      <td>136</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>8.50</td>\n",
       "      <td>110.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>19</td>\n",
       "      <td>25</td>\n",
       "      <td>17710.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>audi</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>wagon</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>105.8</td>\n",
       "      <td>...</td>\n",
       "      <td>136</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>8.50</td>\n",
       "      <td>110.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>19</td>\n",
       "      <td>25</td>\n",
       "      <td>18920.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1</td>\n",
       "      <td>158.0</td>\n",
       "      <td>audi</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>105.8</td>\n",
       "      <td>...</td>\n",
       "      <td>131</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.13</td>\n",
       "      <td>3.40</td>\n",
       "      <td>8.30</td>\n",
       "      <td>140.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>17</td>\n",
       "      <td>20</td>\n",
       "      <td>23875.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>audi</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>4wd</td>\n",
       "      <td>front</td>\n",
       "      <td>99.5</td>\n",
       "      <td>...</td>\n",
       "      <td>131</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.13</td>\n",
       "      <td>3.40</td>\n",
       "      <td>7.00</td>\n",
       "      <td>160.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>16</td>\n",
       "      <td>22</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2</td>\n",
       "      <td>192.0</td>\n",
       "      <td>bmw</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>101.2</td>\n",
       "      <td>...</td>\n",
       "      <td>108</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.50</td>\n",
       "      <td>2.80</td>\n",
       "      <td>8.80</td>\n",
       "      <td>101.0</td>\n",
       "      <td>5800.0</td>\n",
       "      <td>23</td>\n",
       "      <td>29</td>\n",
       "      <td>16430.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0</td>\n",
       "      <td>192.0</td>\n",
       "      <td>bmw</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>101.2</td>\n",
       "      <td>...</td>\n",
       "      <td>108</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.50</td>\n",
       "      <td>2.80</td>\n",
       "      <td>8.80</td>\n",
       "      <td>101.0</td>\n",
       "      <td>5800.0</td>\n",
       "      <td>23</td>\n",
       "      <td>29</td>\n",
       "      <td>16925.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>bmw</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>101.2</td>\n",
       "      <td>...</td>\n",
       "      <td>164</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.31</td>\n",
       "      <td>3.19</td>\n",
       "      <td>9.00</td>\n",
       "      <td>121.0</td>\n",
       "      <td>4250.0</td>\n",
       "      <td>21</td>\n",
       "      <td>28</td>\n",
       "      <td>20970.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0</td>\n",
       "      <td>188.0</td>\n",
       "      <td>bmw</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>101.2</td>\n",
       "      <td>...</td>\n",
       "      <td>164</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.31</td>\n",
       "      <td>3.19</td>\n",
       "      <td>9.00</td>\n",
       "      <td>121.0</td>\n",
       "      <td>4250.0</td>\n",
       "      <td>21</td>\n",
       "      <td>28</td>\n",
       "      <td>21105.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>bmw</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>103.5</td>\n",
       "      <td>...</td>\n",
       "      <td>164</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.31</td>\n",
       "      <td>3.19</td>\n",
       "      <td>9.00</td>\n",
       "      <td>121.0</td>\n",
       "      <td>4250.0</td>\n",
       "      <td>20</td>\n",
       "      <td>25</td>\n",
       "      <td>24565.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>bmw</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>103.5</td>\n",
       "      <td>...</td>\n",
       "      <td>209</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.62</td>\n",
       "      <td>3.39</td>\n",
       "      <td>8.00</td>\n",
       "      <td>182.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>16</td>\n",
       "      <td>22</td>\n",
       "      <td>30760.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>bmw</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>103.5</td>\n",
       "      <td>...</td>\n",
       "      <td>209</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.62</td>\n",
       "      <td>3.39</td>\n",
       "      <td>8.00</td>\n",
       "      <td>182.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>16</td>\n",
       "      <td>22</td>\n",
       "      <td>41315.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>bmw</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>110.0</td>\n",
       "      <td>...</td>\n",
       "      <td>209</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.62</td>\n",
       "      <td>3.39</td>\n",
       "      <td>8.00</td>\n",
       "      <td>182.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>36880.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2</td>\n",
       "      <td>121.0</td>\n",
       "      <td>chevrolet</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>88.4</td>\n",
       "      <td>...</td>\n",
       "      <td>61</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>2.91</td>\n",
       "      <td>3.03</td>\n",
       "      <td>9.50</td>\n",
       "      <td>48.0</td>\n",
       "      <td>5100.0</td>\n",
       "      <td>47</td>\n",
       "      <td>53</td>\n",
       "      <td>5151.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1</td>\n",
       "      <td>98.0</td>\n",
       "      <td>chevrolet</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>94.5</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>3.03</td>\n",
       "      <td>3.11</td>\n",
       "      <td>9.60</td>\n",
       "      <td>70.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>38</td>\n",
       "      <td>43</td>\n",
       "      <td>6295.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>chevrolet</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>94.5</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>3.03</td>\n",
       "      <td>3.11</td>\n",
       "      <td>9.60</td>\n",
       "      <td>70.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>38</td>\n",
       "      <td>43</td>\n",
       "      <td>6575.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1</td>\n",
       "      <td>118.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>93.7</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>2.97</td>\n",
       "      <td>3.23</td>\n",
       "      <td>9.41</td>\n",
       "      <td>68.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>37</td>\n",
       "      <td>41</td>\n",
       "      <td>5572.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1</td>\n",
       "      <td>118.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>93.7</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>2.97</td>\n",
       "      <td>3.23</td>\n",
       "      <td>9.40</td>\n",
       "      <td>68.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>31</td>\n",
       "      <td>38</td>\n",
       "      <td>6377.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1</td>\n",
       "      <td>118.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>93.7</td>\n",
       "      <td>...</td>\n",
       "      <td>98</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.03</td>\n",
       "      <td>3.39</td>\n",
       "      <td>7.60</td>\n",
       "      <td>102.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>24</td>\n",
       "      <td>30</td>\n",
       "      <td>7957.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1</td>\n",
       "      <td>148.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>93.7</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>2.97</td>\n",
       "      <td>3.23</td>\n",
       "      <td>9.40</td>\n",
       "      <td>68.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>31</td>\n",
       "      <td>38</td>\n",
       "      <td>6229.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1</td>\n",
       "      <td>148.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>93.7</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>2.97</td>\n",
       "      <td>3.23</td>\n",
       "      <td>9.40</td>\n",
       "      <td>68.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>31</td>\n",
       "      <td>38</td>\n",
       "      <td>6692.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1</td>\n",
       "      <td>148.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>93.7</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>2.97</td>\n",
       "      <td>3.23</td>\n",
       "      <td>9.40</td>\n",
       "      <td>68.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>31</td>\n",
       "      <td>38</td>\n",
       "      <td>7609.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1</td>\n",
       "      <td>148.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>NaN</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>93.7</td>\n",
       "      <td>...</td>\n",
       "      <td>98</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.03</td>\n",
       "      <td>3.39</td>\n",
       "      <td>7.60</td>\n",
       "      <td>102.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>24</td>\n",
       "      <td>30</td>\n",
       "      <td>8558.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>-1</td>\n",
       "      <td>110.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>wagon</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>103.3</td>\n",
       "      <td>...</td>\n",
       "      <td>122</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>3.34</td>\n",
       "      <td>3.46</td>\n",
       "      <td>8.50</td>\n",
       "      <td>88.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>24</td>\n",
       "      <td>30</td>\n",
       "      <td>8921.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>3</td>\n",
       "      <td>145.0</td>\n",
       "      <td>dodge</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>95.9</td>\n",
       "      <td>...</td>\n",
       "      <td>156</td>\n",
       "      <td>mfi</td>\n",
       "      <td>3.60</td>\n",
       "      <td>3.90</td>\n",
       "      <td>7.00</td>\n",
       "      <td>145.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>19</td>\n",
       "      <td>24</td>\n",
       "      <td>12964.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>175</th>\n",
       "      <td>-1</td>\n",
       "      <td>65.0</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>102.4</td>\n",
       "      <td>...</td>\n",
       "      <td>122</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.31</td>\n",
       "      <td>3.54</td>\n",
       "      <td>8.70</td>\n",
       "      <td>92.0</td>\n",
       "      <td>4200.0</td>\n",
       "      <td>27</td>\n",
       "      <td>32</td>\n",
       "      <td>9988.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>176</th>\n",
       "      <td>-1</td>\n",
       "      <td>65.0</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>102.4</td>\n",
       "      <td>...</td>\n",
       "      <td>122</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.31</td>\n",
       "      <td>3.54</td>\n",
       "      <td>8.70</td>\n",
       "      <td>92.0</td>\n",
       "      <td>4200.0</td>\n",
       "      <td>27</td>\n",
       "      <td>32</td>\n",
       "      <td>10898.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>177</th>\n",
       "      <td>-1</td>\n",
       "      <td>65.0</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>102.4</td>\n",
       "      <td>...</td>\n",
       "      <td>122</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.31</td>\n",
       "      <td>3.54</td>\n",
       "      <td>8.70</td>\n",
       "      <td>92.0</td>\n",
       "      <td>4200.0</td>\n",
       "      <td>27</td>\n",
       "      <td>32</td>\n",
       "      <td>11248.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>178</th>\n",
       "      <td>3</td>\n",
       "      <td>197.0</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>102.9</td>\n",
       "      <td>...</td>\n",
       "      <td>171</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.27</td>\n",
       "      <td>3.35</td>\n",
       "      <td>9.30</td>\n",
       "      <td>161.0</td>\n",
       "      <td>5200.0</td>\n",
       "      <td>20</td>\n",
       "      <td>24</td>\n",
       "      <td>16558.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>179</th>\n",
       "      <td>3</td>\n",
       "      <td>197.0</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>102.9</td>\n",
       "      <td>...</td>\n",
       "      <td>171</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.27</td>\n",
       "      <td>3.35</td>\n",
       "      <td>9.30</td>\n",
       "      <td>161.0</td>\n",
       "      <td>5200.0</td>\n",
       "      <td>19</td>\n",
       "      <td>24</td>\n",
       "      <td>15998.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>180</th>\n",
       "      <td>-1</td>\n",
       "      <td>90.0</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.5</td>\n",
       "      <td>...</td>\n",
       "      <td>171</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.27</td>\n",
       "      <td>3.35</td>\n",
       "      <td>9.20</td>\n",
       "      <td>156.0</td>\n",
       "      <td>5200.0</td>\n",
       "      <td>20</td>\n",
       "      <td>24</td>\n",
       "      <td>15690.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>181</th>\n",
       "      <td>-1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>wagon</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.5</td>\n",
       "      <td>...</td>\n",
       "      <td>161</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.27</td>\n",
       "      <td>3.35</td>\n",
       "      <td>9.20</td>\n",
       "      <td>156.0</td>\n",
       "      <td>5200.0</td>\n",
       "      <td>19</td>\n",
       "      <td>24</td>\n",
       "      <td>15750.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>182</th>\n",
       "      <td>2</td>\n",
       "      <td>122.0</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>diesel</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.3</td>\n",
       "      <td>...</td>\n",
       "      <td>97</td>\n",
       "      <td>idi</td>\n",
       "      <td>3.01</td>\n",
       "      <td>3.40</td>\n",
       "      <td>23.00</td>\n",
       "      <td>52.0</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>37</td>\n",
       "      <td>46</td>\n",
       "      <td>7775.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>183</th>\n",
       "      <td>2</td>\n",
       "      <td>122.0</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.3</td>\n",
       "      <td>...</td>\n",
       "      <td>109</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>9.00</td>\n",
       "      <td>85.0</td>\n",
       "      <td>5250.0</td>\n",
       "      <td>27</td>\n",
       "      <td>34</td>\n",
       "      <td>7975.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>184</th>\n",
       "      <td>2</td>\n",
       "      <td>94.0</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>diesel</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.3</td>\n",
       "      <td>...</td>\n",
       "      <td>97</td>\n",
       "      <td>idi</td>\n",
       "      <td>3.01</td>\n",
       "      <td>3.40</td>\n",
       "      <td>23.00</td>\n",
       "      <td>52.0</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>37</td>\n",
       "      <td>46</td>\n",
       "      <td>7995.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>185</th>\n",
       "      <td>2</td>\n",
       "      <td>94.0</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.3</td>\n",
       "      <td>...</td>\n",
       "      <td>109</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>9.00</td>\n",
       "      <td>85.0</td>\n",
       "      <td>5250.0</td>\n",
       "      <td>27</td>\n",
       "      <td>34</td>\n",
       "      <td>8195.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>186</th>\n",
       "      <td>2</td>\n",
       "      <td>94.0</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.3</td>\n",
       "      <td>...</td>\n",
       "      <td>109</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>9.00</td>\n",
       "      <td>85.0</td>\n",
       "      <td>5250.0</td>\n",
       "      <td>27</td>\n",
       "      <td>34</td>\n",
       "      <td>8495.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>187</th>\n",
       "      <td>2</td>\n",
       "      <td>94.0</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>diesel</td>\n",
       "      <td>turbo</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.3</td>\n",
       "      <td>...</td>\n",
       "      <td>97</td>\n",
       "      <td>idi</td>\n",
       "      <td>3.01</td>\n",
       "      <td>3.40</td>\n",
       "      <td>23.00</td>\n",
       "      <td>68.0</td>\n",
       "      <td>4500.0</td>\n",
       "      <td>37</td>\n",
       "      <td>42</td>\n",
       "      <td>9495.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>188</th>\n",
       "      <td>2</td>\n",
       "      <td>94.0</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.3</td>\n",
       "      <td>...</td>\n",
       "      <td>109</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>10.00</td>\n",
       "      <td>100.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>26</td>\n",
       "      <td>32</td>\n",
       "      <td>9995.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>189</th>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>convertible</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>94.5</td>\n",
       "      <td>...</td>\n",
       "      <td>109</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>8.50</td>\n",
       "      <td>90.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>24</td>\n",
       "      <td>29</td>\n",
       "      <td>11595.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>190</th>\n",
       "      <td>3</td>\n",
       "      <td>256.0</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>94.5</td>\n",
       "      <td>...</td>\n",
       "      <td>109</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>8.50</td>\n",
       "      <td>90.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>24</td>\n",
       "      <td>29</td>\n",
       "      <td>9980.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>191</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>100.4</td>\n",
       "      <td>...</td>\n",
       "      <td>136</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>8.50</td>\n",
       "      <td>110.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>19</td>\n",
       "      <td>24</td>\n",
       "      <td>13295.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>192</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>diesel</td>\n",
       "      <td>turbo</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>100.4</td>\n",
       "      <td>...</td>\n",
       "      <td>97</td>\n",
       "      <td>idi</td>\n",
       "      <td>3.01</td>\n",
       "      <td>3.40</td>\n",
       "      <td>23.00</td>\n",
       "      <td>68.0</td>\n",
       "      <td>4500.0</td>\n",
       "      <td>33</td>\n",
       "      <td>38</td>\n",
       "      <td>13845.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>193</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>volkswagen</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>wagon</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>100.4</td>\n",
       "      <td>...</td>\n",
       "      <td>109</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.40</td>\n",
       "      <td>9.00</td>\n",
       "      <td>88.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>25</td>\n",
       "      <td>31</td>\n",
       "      <td>12290.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>194</th>\n",
       "      <td>-2</td>\n",
       "      <td>103.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.3</td>\n",
       "      <td>...</td>\n",
       "      <td>141</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.78</td>\n",
       "      <td>3.15</td>\n",
       "      <td>9.50</td>\n",
       "      <td>114.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>23</td>\n",
       "      <td>28</td>\n",
       "      <td>12940.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>195</th>\n",
       "      <td>-1</td>\n",
       "      <td>74.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>wagon</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.3</td>\n",
       "      <td>...</td>\n",
       "      <td>141</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.78</td>\n",
       "      <td>3.15</td>\n",
       "      <td>9.50</td>\n",
       "      <td>114.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>23</td>\n",
       "      <td>28</td>\n",
       "      <td>13415.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>196</th>\n",
       "      <td>-2</td>\n",
       "      <td>103.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.3</td>\n",
       "      <td>...</td>\n",
       "      <td>141</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.78</td>\n",
       "      <td>3.15</td>\n",
       "      <td>9.50</td>\n",
       "      <td>114.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>24</td>\n",
       "      <td>28</td>\n",
       "      <td>15985.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>197</th>\n",
       "      <td>-1</td>\n",
       "      <td>74.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>wagon</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.3</td>\n",
       "      <td>...</td>\n",
       "      <td>141</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.78</td>\n",
       "      <td>3.15</td>\n",
       "      <td>9.50</td>\n",
       "      <td>114.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>24</td>\n",
       "      <td>28</td>\n",
       "      <td>16515.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>198</th>\n",
       "      <td>-2</td>\n",
       "      <td>103.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.3</td>\n",
       "      <td>...</td>\n",
       "      <td>130</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.62</td>\n",
       "      <td>3.15</td>\n",
       "      <td>7.50</td>\n",
       "      <td>162.0</td>\n",
       "      <td>5100.0</td>\n",
       "      <td>17</td>\n",
       "      <td>22</td>\n",
       "      <td>18420.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199</th>\n",
       "      <td>-1</td>\n",
       "      <td>74.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>four</td>\n",
       "      <td>wagon</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.3</td>\n",
       "      <td>...</td>\n",
       "      <td>130</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.62</td>\n",
       "      <td>3.15</td>\n",
       "      <td>7.50</td>\n",
       "      <td>162.0</td>\n",
       "      <td>5100.0</td>\n",
       "      <td>17</td>\n",
       "      <td>22</td>\n",
       "      <td>18950.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>200</th>\n",
       "      <td>-1</td>\n",
       "      <td>95.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>109.1</td>\n",
       "      <td>...</td>\n",
       "      <td>141</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.78</td>\n",
       "      <td>3.15</td>\n",
       "      <td>9.50</td>\n",
       "      <td>114.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>23</td>\n",
       "      <td>28</td>\n",
       "      <td>16845.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>201</th>\n",
       "      <td>-1</td>\n",
       "      <td>95.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>109.1</td>\n",
       "      <td>...</td>\n",
       "      <td>141</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.78</td>\n",
       "      <td>3.15</td>\n",
       "      <td>8.70</td>\n",
       "      <td>160.0</td>\n",
       "      <td>5300.0</td>\n",
       "      <td>19</td>\n",
       "      <td>25</td>\n",
       "      <td>19045.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>202</th>\n",
       "      <td>-1</td>\n",
       "      <td>95.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>109.1</td>\n",
       "      <td>...</td>\n",
       "      <td>173</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.58</td>\n",
       "      <td>2.87</td>\n",
       "      <td>8.80</td>\n",
       "      <td>134.0</td>\n",
       "      <td>5500.0</td>\n",
       "      <td>18</td>\n",
       "      <td>23</td>\n",
       "      <td>21485.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>203</th>\n",
       "      <td>-1</td>\n",
       "      <td>95.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>diesel</td>\n",
       "      <td>turbo</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>109.1</td>\n",
       "      <td>...</td>\n",
       "      <td>145</td>\n",
       "      <td>idi</td>\n",
       "      <td>3.01</td>\n",
       "      <td>3.40</td>\n",
       "      <td>23.00</td>\n",
       "      <td>106.0</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>26</td>\n",
       "      <td>27</td>\n",
       "      <td>22470.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>204</th>\n",
       "      <td>-1</td>\n",
       "      <td>95.0</td>\n",
       "      <td>volvo</td>\n",
       "      <td>gas</td>\n",
       "      <td>turbo</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>109.1</td>\n",
       "      <td>...</td>\n",
       "      <td>141</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.78</td>\n",
       "      <td>3.15</td>\n",
       "      <td>9.50</td>\n",
       "      <td>114.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>19</td>\n",
       "      <td>25</td>\n",
       "      <td>22625.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>205 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     symboling  normalized_losses         make fuel_type aspiration num_doors  \\\n",
       "0            3                NaN  alfa-romero       gas        std       two   \n",
       "1            3                NaN  alfa-romero       gas        std       two   \n",
       "2            1                NaN  alfa-romero       gas        std       two   \n",
       "3            2              164.0         audi       gas        std      four   \n",
       "4            2              164.0         audi       gas        std      four   \n",
       "5            2                NaN         audi       gas        std       two   \n",
       "6            1              158.0         audi       gas        std      four   \n",
       "7            1                NaN         audi       gas        std      four   \n",
       "8            1              158.0         audi       gas      turbo      four   \n",
       "9            0                NaN         audi       gas      turbo       two   \n",
       "10           2              192.0          bmw       gas        std       two   \n",
       "11           0              192.0          bmw       gas        std      four   \n",
       "12           0              188.0          bmw       gas        std       two   \n",
       "13           0              188.0          bmw       gas        std      four   \n",
       "14           1                NaN          bmw       gas        std      four   \n",
       "15           0                NaN          bmw       gas        std      four   \n",
       "16           0                NaN          bmw       gas        std       two   \n",
       "17           0                NaN          bmw       gas        std      four   \n",
       "18           2              121.0    chevrolet       gas        std       two   \n",
       "19           1               98.0    chevrolet       gas        std       two   \n",
       "20           0               81.0    chevrolet       gas        std      four   \n",
       "21           1              118.0        dodge       gas        std       two   \n",
       "22           1              118.0        dodge       gas        std       two   \n",
       "23           1              118.0        dodge       gas      turbo       two   \n",
       "24           1              148.0        dodge       gas        std      four   \n",
       "25           1              148.0        dodge       gas        std      four   \n",
       "26           1              148.0        dodge       gas        std      four   \n",
       "27           1              148.0        dodge       gas      turbo       NaN   \n",
       "28          -1              110.0        dodge       gas        std      four   \n",
       "29           3              145.0        dodge       gas      turbo       two   \n",
       "..         ...                ...          ...       ...        ...       ...   \n",
       "175         -1               65.0       toyota       gas        std      four   \n",
       "176         -1               65.0       toyota       gas        std      four   \n",
       "177         -1               65.0       toyota       gas        std      four   \n",
       "178          3              197.0       toyota       gas        std       two   \n",
       "179          3              197.0       toyota       gas        std       two   \n",
       "180         -1               90.0       toyota       gas        std      four   \n",
       "181         -1                NaN       toyota       gas        std      four   \n",
       "182          2              122.0   volkswagen    diesel        std       two   \n",
       "183          2              122.0   volkswagen       gas        std       two   \n",
       "184          2               94.0   volkswagen    diesel        std      four   \n",
       "185          2               94.0   volkswagen       gas        std      four   \n",
       "186          2               94.0   volkswagen       gas        std      four   \n",
       "187          2               94.0   volkswagen    diesel      turbo      four   \n",
       "188          2               94.0   volkswagen       gas        std      four   \n",
       "189          3                NaN   volkswagen       gas        std       two   \n",
       "190          3              256.0   volkswagen       gas        std       two   \n",
       "191          0                NaN   volkswagen       gas        std      four   \n",
       "192          0                NaN   volkswagen    diesel      turbo      four   \n",
       "193          0                NaN   volkswagen       gas        std      four   \n",
       "194         -2              103.0        volvo       gas        std      four   \n",
       "195         -1               74.0        volvo       gas        std      four   \n",
       "196         -2              103.0        volvo       gas        std      four   \n",
       "197         -1               74.0        volvo       gas        std      four   \n",
       "198         -2              103.0        volvo       gas      turbo      four   \n",
       "199         -1               74.0        volvo       gas      turbo      four   \n",
       "200         -1               95.0        volvo       gas        std      four   \n",
       "201         -1               95.0        volvo       gas      turbo      four   \n",
       "202         -1               95.0        volvo       gas        std      four   \n",
       "203         -1               95.0        volvo    diesel      turbo      four   \n",
       "204         -1               95.0        volvo       gas      turbo      four   \n",
       "\n",
       "      body_style drive_wheels engine_location  wheel_base  ...  engine_size  \\\n",
       "0    convertible          rwd           front        88.6  ...          130   \n",
       "1    convertible          rwd           front        88.6  ...          130   \n",
       "2      hatchback          rwd           front        94.5  ...          152   \n",
       "3          sedan          fwd           front        99.8  ...          109   \n",
       "4          sedan          4wd           front        99.4  ...          136   \n",
       "5          sedan          fwd           front        99.8  ...          136   \n",
       "6          sedan          fwd           front       105.8  ...          136   \n",
       "7          wagon          fwd           front       105.8  ...          136   \n",
       "8          sedan          fwd           front       105.8  ...          131   \n",
       "9      hatchback          4wd           front        99.5  ...          131   \n",
       "10         sedan          rwd           front       101.2  ...          108   \n",
       "11         sedan          rwd           front       101.2  ...          108   \n",
       "12         sedan          rwd           front       101.2  ...          164   \n",
       "13         sedan          rwd           front       101.2  ...          164   \n",
       "14         sedan          rwd           front       103.5  ...          164   \n",
       "15         sedan          rwd           front       103.5  ...          209   \n",
       "16         sedan          rwd           front       103.5  ...          209   \n",
       "17         sedan          rwd           front       110.0  ...          209   \n",
       "18     hatchback          fwd           front        88.4  ...           61   \n",
       "19     hatchback          fwd           front        94.5  ...           90   \n",
       "20         sedan          fwd           front        94.5  ...           90   \n",
       "21     hatchback          fwd           front        93.7  ...           90   \n",
       "22     hatchback          fwd           front        93.7  ...           90   \n",
       "23     hatchback          fwd           front        93.7  ...           98   \n",
       "24     hatchback          fwd           front        93.7  ...           90   \n",
       "25         sedan          fwd           front        93.7  ...           90   \n",
       "26         sedan          fwd           front        93.7  ...           90   \n",
       "27         sedan          fwd           front        93.7  ...           98   \n",
       "28         wagon          fwd           front       103.3  ...          122   \n",
       "29     hatchback          fwd           front        95.9  ...          156   \n",
       "..           ...          ...             ...         ...  ...          ...   \n",
       "175    hatchback          fwd           front       102.4  ...          122   \n",
       "176        sedan          fwd           front       102.4  ...          122   \n",
       "177    hatchback          fwd           front       102.4  ...          122   \n",
       "178    hatchback          rwd           front       102.9  ...          171   \n",
       "179    hatchback          rwd           front       102.9  ...          171   \n",
       "180        sedan          rwd           front       104.5  ...          171   \n",
       "181        wagon          rwd           front       104.5  ...          161   \n",
       "182        sedan          fwd           front        97.3  ...           97   \n",
       "183        sedan          fwd           front        97.3  ...          109   \n",
       "184        sedan          fwd           front        97.3  ...           97   \n",
       "185        sedan          fwd           front        97.3  ...          109   \n",
       "186        sedan          fwd           front        97.3  ...          109   \n",
       "187        sedan          fwd           front        97.3  ...           97   \n",
       "188        sedan          fwd           front        97.3  ...          109   \n",
       "189  convertible          fwd           front        94.5  ...          109   \n",
       "190    hatchback          fwd           front        94.5  ...          109   \n",
       "191        sedan          fwd           front       100.4  ...          136   \n",
       "192        sedan          fwd           front       100.4  ...           97   \n",
       "193        wagon          fwd           front       100.4  ...          109   \n",
       "194        sedan          rwd           front       104.3  ...          141   \n",
       "195        wagon          rwd           front       104.3  ...          141   \n",
       "196        sedan          rwd           front       104.3  ...          141   \n",
       "197        wagon          rwd           front       104.3  ...          141   \n",
       "198        sedan          rwd           front       104.3  ...          130   \n",
       "199        wagon          rwd           front       104.3  ...          130   \n",
       "200        sedan          rwd           front       109.1  ...          141   \n",
       "201        sedan          rwd           front       109.1  ...          141   \n",
       "202        sedan          rwd           front       109.1  ...          173   \n",
       "203        sedan          rwd           front       109.1  ...          145   \n",
       "204        sedan          rwd           front       109.1  ...          141   \n",
       "\n",
       "     fuel_system  bore  stroke compression_ratio horsepower  peak_rpm  \\\n",
       "0           mpfi  3.47    2.68              9.00      111.0    5000.0   \n",
       "1           mpfi  3.47    2.68              9.00      111.0    5000.0   \n",
       "2           mpfi  2.68    3.47              9.00      154.0    5000.0   \n",
       "3           mpfi  3.19    3.40             10.00      102.0    5500.0   \n",
       "4           mpfi  3.19    3.40              8.00      115.0    5500.0   \n",
       "5           mpfi  3.19    3.40              8.50      110.0    5500.0   \n",
       "6           mpfi  3.19    3.40              8.50      110.0    5500.0   \n",
       "7           mpfi  3.19    3.40              8.50      110.0    5500.0   \n",
       "8           mpfi  3.13    3.40              8.30      140.0    5500.0   \n",
       "9           mpfi  3.13    3.40              7.00      160.0    5500.0   \n",
       "10          mpfi  3.50    2.80              8.80      101.0    5800.0   \n",
       "11          mpfi  3.50    2.80              8.80      101.0    5800.0   \n",
       "12          mpfi  3.31    3.19              9.00      121.0    4250.0   \n",
       "13          mpfi  3.31    3.19              9.00      121.0    4250.0   \n",
       "14          mpfi  3.31    3.19              9.00      121.0    4250.0   \n",
       "15          mpfi  3.62    3.39              8.00      182.0    5400.0   \n",
       "16          mpfi  3.62    3.39              8.00      182.0    5400.0   \n",
       "17          mpfi  3.62    3.39              8.00      182.0    5400.0   \n",
       "18          2bbl  2.91    3.03              9.50       48.0    5100.0   \n",
       "19          2bbl  3.03    3.11              9.60       70.0    5400.0   \n",
       "20          2bbl  3.03    3.11              9.60       70.0    5400.0   \n",
       "21          2bbl  2.97    3.23              9.41       68.0    5500.0   \n",
       "22          2bbl  2.97    3.23              9.40       68.0    5500.0   \n",
       "23          mpfi  3.03    3.39              7.60      102.0    5500.0   \n",
       "24          2bbl  2.97    3.23              9.40       68.0    5500.0   \n",
       "25          2bbl  2.97    3.23              9.40       68.0    5500.0   \n",
       "26          2bbl  2.97    3.23              9.40       68.0    5500.0   \n",
       "27          mpfi  3.03    3.39              7.60      102.0    5500.0   \n",
       "28          2bbl  3.34    3.46              8.50       88.0    5000.0   \n",
       "29           mfi  3.60    3.90              7.00      145.0    5000.0   \n",
       "..           ...   ...     ...               ...        ...       ...   \n",
       "175         mpfi  3.31    3.54              8.70       92.0    4200.0   \n",
       "176         mpfi  3.31    3.54              8.70       92.0    4200.0   \n",
       "177         mpfi  3.31    3.54              8.70       92.0    4200.0   \n",
       "178         mpfi  3.27    3.35              9.30      161.0    5200.0   \n",
       "179         mpfi  3.27    3.35              9.30      161.0    5200.0   \n",
       "180         mpfi  3.27    3.35              9.20      156.0    5200.0   \n",
       "181         mpfi  3.27    3.35              9.20      156.0    5200.0   \n",
       "182          idi  3.01    3.40             23.00       52.0    4800.0   \n",
       "183         mpfi  3.19    3.40              9.00       85.0    5250.0   \n",
       "184          idi  3.01    3.40             23.00       52.0    4800.0   \n",
       "185         mpfi  3.19    3.40              9.00       85.0    5250.0   \n",
       "186         mpfi  3.19    3.40              9.00       85.0    5250.0   \n",
       "187          idi  3.01    3.40             23.00       68.0    4500.0   \n",
       "188         mpfi  3.19    3.40             10.00      100.0    5500.0   \n",
       "189         mpfi  3.19    3.40              8.50       90.0    5500.0   \n",
       "190         mpfi  3.19    3.40              8.50       90.0    5500.0   \n",
       "191         mpfi  3.19    3.40              8.50      110.0    5500.0   \n",
       "192          idi  3.01    3.40             23.00       68.0    4500.0   \n",
       "193         mpfi  3.19    3.40              9.00       88.0    5500.0   \n",
       "194         mpfi  3.78    3.15              9.50      114.0    5400.0   \n",
       "195         mpfi  3.78    3.15              9.50      114.0    5400.0   \n",
       "196         mpfi  3.78    3.15              9.50      114.0    5400.0   \n",
       "197         mpfi  3.78    3.15              9.50      114.0    5400.0   \n",
       "198         mpfi  3.62    3.15              7.50      162.0    5100.0   \n",
       "199         mpfi  3.62    3.15              7.50      162.0    5100.0   \n",
       "200         mpfi  3.78    3.15              9.50      114.0    5400.0   \n",
       "201         mpfi  3.78    3.15              8.70      160.0    5300.0   \n",
       "202         mpfi  3.58    2.87              8.80      134.0    5500.0   \n",
       "203          idi  3.01    3.40             23.00      106.0    4800.0   \n",
       "204         mpfi  3.78    3.15              9.50      114.0    5400.0   \n",
       "\n",
       "    city_mpg  highway_mpg    price  \n",
       "0         21           27  13495.0  \n",
       "1         21           27  16500.0  \n",
       "2         19           26  16500.0  \n",
       "3         24           30  13950.0  \n",
       "4         18           22  17450.0  \n",
       "5         19           25  15250.0  \n",
       "6         19           25  17710.0  \n",
       "7         19           25  18920.0  \n",
       "8         17           20  23875.0  \n",
       "9         16           22      NaN  \n",
       "10        23           29  16430.0  \n",
       "11        23           29  16925.0  \n",
       "12        21           28  20970.0  \n",
       "13        21           28  21105.0  \n",
       "14        20           25  24565.0  \n",
       "15        16           22  30760.0  \n",
       "16        16           22  41315.0  \n",
       "17        15           20  36880.0  \n",
       "18        47           53   5151.0  \n",
       "19        38           43   6295.0  \n",
       "20        38           43   6575.0  \n",
       "21        37           41   5572.0  \n",
       "22        31           38   6377.0  \n",
       "23        24           30   7957.0  \n",
       "24        31           38   6229.0  \n",
       "25        31           38   6692.0  \n",
       "26        31           38   7609.0  \n",
       "27        24           30   8558.0  \n",
       "28        24           30   8921.0  \n",
       "29        19           24  12964.0  \n",
       "..       ...          ...      ...  \n",
       "175       27           32   9988.0  \n",
       "176       27           32  10898.0  \n",
       "177       27           32  11248.0  \n",
       "178       20           24  16558.0  \n",
       "179       19           24  15998.0  \n",
       "180       20           24  15690.0  \n",
       "181       19           24  15750.0  \n",
       "182       37           46   7775.0  \n",
       "183       27           34   7975.0  \n",
       "184       37           46   7995.0  \n",
       "185       27           34   8195.0  \n",
       "186       27           34   8495.0  \n",
       "187       37           42   9495.0  \n",
       "188       26           32   9995.0  \n",
       "189       24           29  11595.0  \n",
       "190       24           29   9980.0  \n",
       "191       19           24  13295.0  \n",
       "192       33           38  13845.0  \n",
       "193       25           31  12290.0  \n",
       "194       23           28  12940.0  \n",
       "195       23           28  13415.0  \n",
       "196       24           28  15985.0  \n",
       "197       24           28  16515.0  \n",
       "198       17           22  18420.0  \n",
       "199       17           22  18950.0  \n",
       "200       23           28  16845.0  \n",
       "201       19           25  19045.0  \n",
       "202       18           23  21485.0  \n",
       "203       26           27  22470.0  \n",
       "204       19           25  22625.0  \n",
       "\n",
       "[205 rows x 26 columns]"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>body_style</th>\n",
       "      <th>convertible</th>\n",
       "      <th>hardtop</th>\n",
       "      <th>hatchback</th>\n",
       "      <th>sedan</th>\n",
       "      <th>wagon</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>make</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>alfa-romero</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>audi</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bmw</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isuzu</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jaguar</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercedes-benz</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercury</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mitsubishi</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>nissan</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>peugot</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>porsche</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>renault</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>saab</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>subaru</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>14</td>\n",
       "      <td>10</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volvo</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "body_style     convertible  hardtop  hatchback  sedan  wagon\n",
       "make                                                        \n",
       "alfa-romero              2        0          1      0      0\n",
       "audi                     0        0          1      5      1\n",
       "bmw                      0        0          0      8      0\n",
       "chevrolet                0        0          2      1      0\n",
       "dodge                    0        0          5      3      1\n",
       "honda                    0        0          7      5      1\n",
       "isuzu                    0        0          1      3      0\n",
       "jaguar                   0        0          0      3      0\n",
       "mazda                    0        0         10      7      0\n",
       "mercedes-benz            1        2          0      4      1\n",
       "mercury                  0        0          1      0      0\n",
       "mitsubishi               0        0          9      4      0\n",
       "nissan                   0        1          5      9      3\n",
       "peugot                   0        0          0      7      4\n",
       "plymouth                 0        0          4      2      1\n",
       "porsche                  1        2          2      0      0\n",
       "renault                  0        0          1      0      1\n",
       "saab                     0        0          3      3      0\n",
       "subaru                   0        0          3      5      4\n",
       "toyota                   1        3         14     10      4\n",
       "volkswagen               1        0          1      9      1\n",
       "volvo                    0        0          0      8      3"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(df_raw.make, df_raw.body_style)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>num_doors</th>\n",
       "      <th>four</th>\n",
       "      <th>two</th>\n",
       "      <th>Total</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>make</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>alfa-romero</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>audi</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bmw</th>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda</th>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isuzu</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jaguar</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda</th>\n",
       "      <td>7</td>\n",
       "      <td>9</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercedes-benz</th>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercury</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mitsubishi</th>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>nissan</th>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>peugot</th>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth</th>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>porsche</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>renault</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>saab</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>subaru</th>\n",
       "      <td>9</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota</th>\n",
       "      <td>18</td>\n",
       "      <td>14</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen</th>\n",
       "      <td>8</td>\n",
       "      <td>4</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volvo</th>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Total</th>\n",
       "      <td>114</td>\n",
       "      <td>89</td>\n",
       "      <td>203</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "num_doors      four  two  Total\n",
       "make                           \n",
       "alfa-romero       0    3      3\n",
       "audi              5    2      7\n",
       "bmw               5    3      8\n",
       "chevrolet         1    2      3\n",
       "dodge             4    4      8\n",
       "honda             5    8     13\n",
       "isuzu             2    2      4\n",
       "jaguar            2    1      3\n",
       "mazda             7    9     16\n",
       "mercedes-benz     5    3      8\n",
       "mercury           0    1      1\n",
       "mitsubishi        4    9     13\n",
       "nissan            9    9     18\n",
       "peugot           11    0     11\n",
       "plymouth          4    3      7\n",
       "porsche           0    5      5\n",
       "renault           1    1      2\n",
       "saab              3    3      6\n",
       "subaru            9    3     12\n",
       "toyota           18   14     32\n",
       "volkswagen        8    4     12\n",
       "volvo            11    0     11\n",
       "Total           114   89    203"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(df_raw.make, df_raw.num_doors, margins=True, margins_name=\"Total\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>body_style</th>\n",
       "      <th>convertible</th>\n",
       "      <th>hardtop</th>\n",
       "      <th>hatchback</th>\n",
       "      <th>sedan</th>\n",
       "      <th>wagon</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>make</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>alfa-romero</th>\n",
       "      <td>14997.5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16500.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>audi</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17647.0</td>\n",
       "      <td>18920.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bmw</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>26118.8</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5723.0</td>\n",
       "      <td>6575.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7819.8</td>\n",
       "      <td>7619.7</td>\n",
       "      <td>8921.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7054.4</td>\n",
       "      <td>9945.0</td>\n",
       "      <td>7295.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isuzu</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11048.0</td>\n",
       "      <td>6785.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jaguar</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>34600.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10085.0</td>\n",
       "      <td>11464.1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercedes-benz</th>\n",
       "      <td>35056.0</td>\n",
       "      <td>36788.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>33074.0</td>\n",
       "      <td>28248.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercury</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16503.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mitsubishi</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9597.9</td>\n",
       "      <td>8434.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>nissan</th>\n",
       "      <td>NaN</td>\n",
       "      <td>8249.0</td>\n",
       "      <td>14409.0</td>\n",
       "      <td>8604.6</td>\n",
       "      <td>9915.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>peugot</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15758.6</td>\n",
       "      <td>15017.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8130.5</td>\n",
       "      <td>7150.5</td>\n",
       "      <td>8921.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>porsche</th>\n",
       "      <td>37028.0</td>\n",
       "      <td>33278.0</td>\n",
       "      <td>22018.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>renault</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9895.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9295.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>saab</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15013.3</td>\n",
       "      <td>15433.3</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>subaru</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6591.3</td>\n",
       "      <td>9070.6</td>\n",
       "      <td>9342.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota</th>\n",
       "      <td>17669.0</td>\n",
       "      <td>9762.3</td>\n",
       "      <td>9616.0</td>\n",
       "      <td>9542.2</td>\n",
       "      <td>9836.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen</th>\n",
       "      <td>11595.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9980.0</td>\n",
       "      <td>9673.9</td>\n",
       "      <td>12290.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volvo</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18726.9</td>\n",
       "      <td>16293.3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "body_style     convertible  hardtop  hatchback    sedan    wagon\n",
       "make                                                            \n",
       "alfa-romero        14997.5      NaN    16500.0      NaN      NaN\n",
       "audi                   NaN      NaN        NaN  17647.0  18920.0\n",
       "bmw                    NaN      NaN        NaN  26118.8      NaN\n",
       "chevrolet              NaN      NaN     5723.0   6575.0      NaN\n",
       "dodge                  NaN      NaN     7819.8   7619.7   8921.0\n",
       "honda                  NaN      NaN     7054.4   9945.0   7295.0\n",
       "isuzu                  NaN      NaN    11048.0   6785.0      NaN\n",
       "jaguar                 NaN      NaN        NaN  34600.0      NaN\n",
       "mazda                  NaN      NaN    10085.0  11464.1      NaN\n",
       "mercedes-benz      35056.0  36788.0        NaN  33074.0  28248.0\n",
       "mercury                NaN      NaN    16503.0      NaN      NaN\n",
       "mitsubishi             NaN      NaN     9597.9   8434.0      NaN\n",
       "nissan                 NaN   8249.0    14409.0   8604.6   9915.7\n",
       "peugot                 NaN      NaN        NaN  15758.6  15017.5\n",
       "plymouth               NaN      NaN     8130.5   7150.5   8921.0\n",
       "porsche            37028.0  33278.0    22018.0      NaN      NaN\n",
       "renault                NaN      NaN     9895.0      NaN   9295.0\n",
       "saab                   NaN      NaN    15013.3  15433.3      NaN\n",
       "subaru                 NaN      NaN     6591.3   9070.6   9342.0\n",
       "toyota             17669.0   9762.3     9616.0   9542.2   9836.0\n",
       "volkswagen         11595.0      NaN     9980.0   9673.9  12290.0\n",
       "volvo                  NaN      NaN        NaN  18726.9  16293.3"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(df_raw.make, df_raw.body_style, values=df_raw.price, aggfunc='mean').round(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. Tiling\n",
    "* Transforms continues value into discrete value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data.fillna({'Age':29}, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PassengerId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Survived  Pclass  \\\n",
       "PassengerId                     \n",
       "1                   0       3   \n",
       "2                   1       1   \n",
       "3                   1       3   \n",
       "4                   1       1   \n",
       "5                   0       3   \n",
       "\n",
       "                                                          Name     Sex   Age  \\\n",
       "PassengerId                                                                    \n",
       "1                                      Braund, Mr. Owen Harris    male  22.0   \n",
       "2            Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0   \n",
       "3                                       Heikkinen, Miss. Laina  female  26.0   \n",
       "4                 Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0   \n",
       "5                                     Allen, Mr. William Henry    male  35.0   \n",
       "\n",
       "             SibSp  Parch            Ticket     Fare Cabin Embarked  \n",
       "PassengerId                                                          \n",
       "1                1      0         A/5 21171   7.2500   NaN        S  \n",
       "2                1      0          PC 17599  71.2833   C85        C  \n",
       "3                0      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "4                1      0            113803  53.1000  C123        S  \n",
       "5                0      0            373450   8.0500   NaN        S  "
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "80.0"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic_data.Age.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId\n",
       "1      adult\n",
       "2      adult\n",
       "3      adult\n",
       "4      adult\n",
       "5      adult\n",
       "6      adult\n",
       "7      adult\n",
       "8        kid\n",
       "9      adult\n",
       "10       kid\n",
       "11       kid\n",
       "12     adult\n",
       "13       kid\n",
       "14     adult\n",
       "15       kid\n",
       "16     adult\n",
       "17       kid\n",
       "18     adult\n",
       "19     adult\n",
       "20     adult\n",
       "21     adult\n",
       "22     adult\n",
       "23       kid\n",
       "24     adult\n",
       "25       kid\n",
       "26     adult\n",
       "27     adult\n",
       "28       kid\n",
       "29     adult\n",
       "30     adult\n",
       "       ...  \n",
       "862    adult\n",
       "863    adult\n",
       "864    adult\n",
       "865    adult\n",
       "866    adult\n",
       "867    adult\n",
       "868    adult\n",
       "869    adult\n",
       "870      kid\n",
       "871    adult\n",
       "872    adult\n",
       "873    adult\n",
       "874    adult\n",
       "875    adult\n",
       "876      kid\n",
       "877      kid\n",
       "878      kid\n",
       "879    adult\n",
       "880    adult\n",
       "881    adult\n",
       "882    adult\n",
       "883    adult\n",
       "884    adult\n",
       "885    adult\n",
       "886    adult\n",
       "887    adult\n",
       "888      kid\n",
       "889    adult\n",
       "890    adult\n",
       "891    adult\n",
       "Name: Age, Length: 891, dtype: category\n",
       "Categories (3, object): [kid < adult < old]"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_bucket = pd.cut(x = titanic_data.Age, bins=[0,20,60,80],labels=['kid','adult','old'])\n",
    "age_bucket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data['Age_Bucket'] = age_bucket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "      <th>Age_Bucket</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PassengerId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "      <td>adult</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "      <td>adult</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "      <td>adult</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "      <td>adult</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "      <td>adult</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Survived  Pclass  \\\n",
       "PassengerId                     \n",
       "1                   0       3   \n",
       "2                   1       1   \n",
       "3                   1       3   \n",
       "4                   1       1   \n",
       "5                   0       3   \n",
       "\n",
       "                                                          Name     Sex   Age  \\\n",
       "PassengerId                                                                    \n",
       "1                                      Braund, Mr. Owen Harris    male  22.0   \n",
       "2            Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0   \n",
       "3                                       Heikkinen, Miss. Laina  female  26.0   \n",
       "4                 Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0   \n",
       "5                                     Allen, Mr. William Henry    male  35.0   \n",
       "\n",
       "             SibSp  Parch            Ticket     Fare Cabin Embarked Age_Bucket  \n",
       "PassengerId                                                                     \n",
       "1                1      0         A/5 21171   7.2500   NaN        S      adult  \n",
       "2                1      0          PC 17599  71.2833   C85        C      adult  \n",
       "3                0      0  STON/O2. 3101282   7.9250   NaN        S      adult  \n",
       "4                1      0            113803  53.1000  C123        S      adult  \n",
       "5                0      0            373450   8.0500   NaN        S      adult  "
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8. Computing Dummy Variable\n",
    "* Labels into one hot vectors\n",
    "* Convert categorical variable into dummy/indicator variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sex_female</th>\n",
       "      <th>Sex_male</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PassengerId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>862</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>863</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>864</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>865</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>866</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>867</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>868</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>869</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>870</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>871</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>872</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>873</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>874</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>875</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>876</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>877</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>878</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>879</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>880</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>881</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>882</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>883</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>884</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>885</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>887</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>888</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>889</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>890</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>891 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Sex_female  Sex_male\n",
       "PassengerId                      \n",
       "1                     0         1\n",
       "2                     1         0\n",
       "3                     1         0\n",
       "4                     1         0\n",
       "5                     0         1\n",
       "6                     0         1\n",
       "7                     0         1\n",
       "8                     0         1\n",
       "9                     1         0\n",
       "10                    1         0\n",
       "11                    1         0\n",
       "12                    1         0\n",
       "13                    0         1\n",
       "14                    0         1\n",
       "15                    1         0\n",
       "16                    1         0\n",
       "17                    0         1\n",
       "18                    0         1\n",
       "19                    1         0\n",
       "20                    1         0\n",
       "21                    0         1\n",
       "22                    0         1\n",
       "23                    1         0\n",
       "24                    0         1\n",
       "25                    1         0\n",
       "26                    1         0\n",
       "27                    0         1\n",
       "28                    0         1\n",
       "29                    1         0\n",
       "30                    0         1\n",
       "...                 ...       ...\n",
       "862                   0         1\n",
       "863                   1         0\n",
       "864                   1         0\n",
       "865                   0         1\n",
       "866                   1         0\n",
       "867                   1         0\n",
       "868                   0         1\n",
       "869                   0         1\n",
       "870                   0         1\n",
       "871                   0         1\n",
       "872                   1         0\n",
       "873                   0         1\n",
       "874                   0         1\n",
       "875                   1         0\n",
       "876                   1         0\n",
       "877                   0         1\n",
       "878                   0         1\n",
       "879                   0         1\n",
       "880                   1         0\n",
       "881                   1         0\n",
       "882                   0         1\n",
       "883                   1         0\n",
       "884                   0         1\n",
       "885                   0         1\n",
       "886                   1         0\n",
       "887                   0         1\n",
       "888                   1         0\n",
       "889                   1         0\n",
       "890                   0         1\n",
       "891                   0         1\n",
       "\n",
       "[891 rows x 2 columns]"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.get_dummies(titanic_data.Sex, prefix='Sex')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9. Factorize\n",
    "* Labels into categorical values\n",
    "* This method is useful for obtaining a numeric representation of an array when all that matters is identifying distinct values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symboling</th>\n",
       "      <th>normalized_losses</th>\n",
       "      <th>make</th>\n",
       "      <th>fuel_type</th>\n",
       "      <th>aspiration</th>\n",
       "      <th>num_doors</th>\n",
       "      <th>body_style</th>\n",
       "      <th>drive_wheels</th>\n",
       "      <th>engine_location</th>\n",
       "      <th>wheel_base</th>\n",
       "      <th>...</th>\n",
       "      <th>engine_size</th>\n",
       "      <th>fuel_system</th>\n",
       "      <th>bore</th>\n",
       "      <th>stroke</th>\n",
       "      <th>compression_ratio</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>peak_rpm</th>\n",
       "      <th>city_mpg</th>\n",
       "      <th>highway_mpg</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>166</th>\n",
       "      <td>1</td>\n",
       "      <td>168.0</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>94.5</td>\n",
       "      <td>...</td>\n",
       "      <td>98</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.24</td>\n",
       "      <td>3.08</td>\n",
       "      <td>9.4</td>\n",
       "      <td>112.0</td>\n",
       "      <td>6600.0</td>\n",
       "      <td>26</td>\n",
       "      <td>29</td>\n",
       "      <td>9538.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>2</td>\n",
       "      <td>83.0</td>\n",
       "      <td>subaru</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hatchback</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>93.7</td>\n",
       "      <td>...</td>\n",
       "      <td>97</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>3.62</td>\n",
       "      <td>2.36</td>\n",
       "      <td>9.0</td>\n",
       "      <td>69.0</td>\n",
       "      <td>4900.0</td>\n",
       "      <td>31</td>\n",
       "      <td>36</td>\n",
       "      <td>5118.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>0</td>\n",
       "      <td>118.0</td>\n",
       "      <td>mazda</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.9</td>\n",
       "      <td>...</td>\n",
       "      <td>140</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.76</td>\n",
       "      <td>3.16</td>\n",
       "      <td>8.0</td>\n",
       "      <td>120.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>19</td>\n",
       "      <td>27</td>\n",
       "      <td>18280.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>0</td>\n",
       "      <td>102.0</td>\n",
       "      <td>subaru</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.2</td>\n",
       "      <td>...</td>\n",
       "      <td>108</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>3.62</td>\n",
       "      <td>2.64</td>\n",
       "      <td>9.5</td>\n",
       "      <td>82.0</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>32</td>\n",
       "      <td>37</td>\n",
       "      <td>7126.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>181</th>\n",
       "      <td>-1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>wagon</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>104.5</td>\n",
       "      <td>...</td>\n",
       "      <td>161</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.27</td>\n",
       "      <td>3.35</td>\n",
       "      <td>9.2</td>\n",
       "      <td>156.0</td>\n",
       "      <td>5200.0</td>\n",
       "      <td>19</td>\n",
       "      <td>24</td>\n",
       "      <td>15750.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>1</td>\n",
       "      <td>125.0</td>\n",
       "      <td>mitsubishi</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>96.3</td>\n",
       "      <td>...</td>\n",
       "      <td>122</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>3.35</td>\n",
       "      <td>3.46</td>\n",
       "      <td>8.5</td>\n",
       "      <td>88.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>25</td>\n",
       "      <td>32</td>\n",
       "      <td>6989.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>honda</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>96.5</td>\n",
       "      <td>...</td>\n",
       "      <td>110</td>\n",
       "      <td>1bbl</td>\n",
       "      <td>3.15</td>\n",
       "      <td>3.58</td>\n",
       "      <td>9.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>5800.0</td>\n",
       "      <td>27</td>\n",
       "      <td>33</td>\n",
       "      <td>10295.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>2</td>\n",
       "      <td>134.0</td>\n",
       "      <td>toyota</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>two</td>\n",
       "      <td>hardtop</td>\n",
       "      <td>rwd</td>\n",
       "      <td>front</td>\n",
       "      <td>98.4</td>\n",
       "      <td>...</td>\n",
       "      <td>146</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.62</td>\n",
       "      <td>3.50</td>\n",
       "      <td>9.3</td>\n",
       "      <td>116.0</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>24</td>\n",
       "      <td>30</td>\n",
       "      <td>8449.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>chevrolet</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>94.5</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>2bbl</td>\n",
       "      <td>3.03</td>\n",
       "      <td>3.11</td>\n",
       "      <td>9.6</td>\n",
       "      <td>70.0</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>38</td>\n",
       "      <td>43</td>\n",
       "      <td>6575.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>0</td>\n",
       "      <td>102.0</td>\n",
       "      <td>subaru</td>\n",
       "      <td>gas</td>\n",
       "      <td>std</td>\n",
       "      <td>four</td>\n",
       "      <td>sedan</td>\n",
       "      <td>fwd</td>\n",
       "      <td>front</td>\n",
       "      <td>97.2</td>\n",
       "      <td>...</td>\n",
       "      <td>108</td>\n",
       "      <td>mpfi</td>\n",
       "      <td>3.62</td>\n",
       "      <td>2.64</td>\n",
       "      <td>9.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>5200.0</td>\n",
       "      <td>26</td>\n",
       "      <td>32</td>\n",
       "      <td>9960.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     symboling  normalized_losses        make fuel_type aspiration num_doors  \\\n",
       "166          1              168.0      toyota       gas        std       two   \n",
       "138          2               83.0      subaru       gas        std       two   \n",
       "65           0              118.0       mazda       gas        std      four   \n",
       "141          0              102.0      subaru       gas        std      four   \n",
       "181         -1                NaN      toyota       gas        std      four   \n",
       "85           1              125.0  mitsubishi       gas        std      four   \n",
       "40           0               85.0       honda       gas        std      four   \n",
       "167          2              134.0      toyota       gas        std       two   \n",
       "20           0               81.0   chevrolet       gas        std      four   \n",
       "143          0              102.0      subaru       gas        std      four   \n",
       "\n",
       "    body_style drive_wheels engine_location  wheel_base  ...  engine_size  \\\n",
       "166  hatchback          rwd           front        94.5  ...           98   \n",
       "138  hatchback          fwd           front        93.7  ...           97   \n",
       "65       sedan          rwd           front       104.9  ...          140   \n",
       "141      sedan          fwd           front        97.2  ...          108   \n",
       "181      wagon          rwd           front       104.5  ...          161   \n",
       "85       sedan          fwd           front        96.3  ...          122   \n",
       "40       sedan          fwd           front        96.5  ...          110   \n",
       "167    hardtop          rwd           front        98.4  ...          146   \n",
       "20       sedan          fwd           front        94.5  ...           90   \n",
       "143      sedan          fwd           front        97.2  ...          108   \n",
       "\n",
       "     fuel_system  bore  stroke compression_ratio horsepower  peak_rpm  \\\n",
       "166         mpfi  3.24    3.08               9.4      112.0    6600.0   \n",
       "138         2bbl  3.62    2.36               9.0       69.0    4900.0   \n",
       "65          mpfi  3.76    3.16               8.0      120.0    5000.0   \n",
       "141         2bbl  3.62    2.64               9.5       82.0    4800.0   \n",
       "181         mpfi  3.27    3.35               9.2      156.0    5200.0   \n",
       "85          2bbl  3.35    3.46               8.5       88.0    5000.0   \n",
       "40          1bbl  3.15    3.58               9.0       86.0    5800.0   \n",
       "167         mpfi  3.62    3.50               9.3      116.0    4800.0   \n",
       "20          2bbl  3.03    3.11               9.6       70.0    5400.0   \n",
       "143         mpfi  3.62    2.64               9.0       94.0    5200.0   \n",
       "\n",
       "    city_mpg  highway_mpg    price  \n",
       "166       26           29   9538.0  \n",
       "138       31           36   5118.0  \n",
       "65        19           27  18280.0  \n",
       "141       32           37   7126.0  \n",
       "181       19           24  15750.0  \n",
       "85        25           32   6989.0  \n",
       "40        27           33  10295.0  \n",
       "167       24           30   8449.0  \n",
       "20        38           43   6575.0  \n",
       "143       26           32   9960.0  \n",
       "\n",
       "[10 rows x 26 columns]"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_raw.sample(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,\n",
       "        1, 0, 1, 1, 1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 1, 1,\n",
       "        1, 1, 4, 0, 0, 0, 1, 1, 1, 1, 1, 5, 5, 5, 0, 1, 1, 1, 1, 6, 1, 0,\n",
       "        6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 1, 1, 1, 7, 7, 1, 7, 7, 7, 1, 1, 7,\n",
       "        7, 1, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 6, 0,\n",
       "        6, 0, 6, 0, 6, 0, 6, 0, 1, 7, 1, 1, 1, 1, 7, 0, 0, 0, 0, 0, 0, 0,\n",
       "        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1,\n",
       "        1, 1, 1, 1, 6, 6, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0,\n",
       "        0, 0, 0, 0, 0, 0, 6, 0, 6, 0, 0, 6, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0,\n",
       "        0, 0, 0, 0, 0, 6, 0]),\n",
       " Index(['mpfi', '2bbl', 'mfi', '1bbl', 'spfi', '4bbl', 'idi', 'spdi'], dtype='object'))"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.factorize(df_raw.fuel_system)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10. Exploding data\n",
    "* Transforming values of column containing list-like information ro multiple rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'Name':['Abhi','Mac','Ram'],'Marks':[[87,73,22],[22,11],[44,55,66]]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Marks</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Abhi</td>\n",
       "      <td>[87, 73, 22]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Mac</td>\n",
       "      <td>[22, 11]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ram</td>\n",
       "      <td>[44, 55, 66]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Name         Marks\n",
       "0  Abhi  [87, 73, 22]\n",
       "1   Mac      [22, 11]\n",
       "2   Ram  [44, 55, 66]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Marks</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Abhi</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Abhi</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Abhi</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Mac</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Mac</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ram</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ram</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ram</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Name Marks\n",
       "0  Abhi    87\n",
       "0  Abhi    73\n",
       "0  Abhi    22\n",
       "1   Mac    22\n",
       "1   Mac    11\n",
       "2   Ram    44\n",
       "2   Ram    55\n",
       "2   Ram    66"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.explode('Marks')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
